从Angularjs控制器和服务Javascript Promise问题的for循环中调用多个$ http服务 [英] Calling multiple $http services in a for loop from Angularjs controller and services javascript promise issue

查看:80
本文介绍了从Angularjs控制器和服务Javascript Promise问题的for循环中调用多个$ http服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Angularjs应用程序具有一个控制器并为Java脚本提供服务.服务使用$ http调用WCF REST服务.我需要基于For循环中使用的对象中的数据在For循环中调用多个服务.下面是场景:

My Angularjs application have a controller and services java scripts. Services call WCF REST services using $http. I need to call multiple services in a For loop based on data in object used in For loop. Below is the scenario:

我想将几个文件上传到服务器上的文件夹,并将文件名保存到数据库.我有一个文件详细信息数组,用户选择要上传.我要验证数据库中已经存在的文件名(基于EntityId),然后在上载到服务器之前重命名新文件."因此,下面是for循环和服务调用:

"I want to upload few files to a folder on server and save file names to database. I have an array of file details that user selected to upload. I want to verify file names already in database (based on EntityId) then rename new file before uploading to server." So below is the for loop and service calls:

for (var i = 0; i < filesDataToUpload.length; i++) {

        var fileDataToUpload = filesDataToUpload[i];

        FileUploadService.GetUploadDetailsByEntityId(fileDataToUpload.ENTITY_ID).success(function (response) {
            $scope.UploadDetailsByEntity = response;
        });

        var newFileName = VerifyAndRenameBeforeSave(fileDataToUpload, $scope.UploadDetailsByEntity);

        FileUploadService.UploadFile(newFileName, fileData).success(function (data) {
            //some code
        }).error(function (error) {
            $scope.error("An error occured while uploading file -" + error.ExceptionMessage);
            isSuccess = false;
        });

        FileUploadService.InsertUploadDetails(FileUploadDetails).success(function (data) {
            //some code
        }).error(function (error) {
            $scope.error("An error occured while creating the Entity -" + error.ExceptionMessage);
            isSuccess = false;
        });

    }

问题: 问题:

我的问题是-我没有从我想在随后的验证和重命名文件以及保存到数据库的方法中使用的第一个服务调用中得到响应.我发现这是因为$ http的诺言行为.因此,我移动了文件验证和重命名方法,并将部分功能保存在

My problem is - I do not get the response from first service call that I want to use in subsequent methods of verifying and renaming the files and saving to database. I found that it is because of the promise behavior of $http. So I moved my file verify and renaming methods and save part of functionality inside the

.success(function(response){
//verify that file does not already exists and rename it to the next number of file
//upload the file to the server

//Save new file name and other details to the database
}

但是它只能在第一次使用,而不能在for循环的下一次迭代中使用.对于第二次迭代,它将检索第一个EntityId的记录.

But it works only for first time and not the next iteration of the for loop. For second iteration it retrives the record for first EntityId.

我已经检查了

有人可以帮助我解决此问题吗?

Can any one help me to get this resolved?

推荐答案

之所以发生这种情况,是因为 .success 函数在实际上等待异步任务的响应时返回了一个Promise.像这样尝试-

It's happening because the .success function returns a promise while it actually waits for the async task's response. Try it like this -

for (var i = 0; i < filesDataToUpload.length; i++) {
        var fileDataToUpload = filesDataToUpload[i];
        FileUploadService.GetUploadDetailsByEntityId(fileDataToUpload.ENTITY_ID).success(function (response) {
            $scope.UploadDetailsByEntity = response;
            var newFileName = VerifyAndRenameBeforeSave(fileDataToUpload, $scope.UploadDetailsByEntity);

            FileUploadService.UploadFile(newFileName, fileData).success(function (data) {
                    //some code
                    FileUploadService.InsertUploadDetails(FileUploadDetails).success(function (data) {
                            //some code
                    }).error(function (error) {
                            $scope.error("An error occured while creating the Entity -" + error.ExceptionMessage);
                            isSuccess = false;
                    });
            }).error(function (error) {
                    $scope.error("An error occured while uploading file -" + error.ExceptionMessage);
                    isSuccess = false;
            });
        });
}

这篇关于从Angularjs控制器和服务Javascript Promise问题的for循环中调用多个$ http服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆