Skip to content Skip to sidebar Skip to footer

Calling Multiple $http Services In A For Loop From Angularjs Controller And Services Javascript Promise Issue

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 o

Solution 1:

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;
            });
        });
}

Post a Comment for "Calling Multiple $http Services In A For Loop From Angularjs Controller And Services Javascript Promise Issue"