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

查看:24
本文介绍了从 Angularjs 控制器和服务 javascript 承诺问题在 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 承诺问题在 for 循环中调用多个 $http 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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