How to Safely call an promise-based AngularJS service in a loop?

56 Views Asked by At

I am calling my Angular service as follows:

$scope.testData.length = 10;
for (var k = 0; k <= $scope.testData.length; k++) { 
    myTestService.saveData({
        Id: $scope.id,
        Description: $scope.jobdescrtiption,
        FirstName: $scope.firstname,
        LastName: $scope.lastname,
        Address: $scope.address,
        Phone: $scope.phone,
        Email: $scope.email                 
    }).then(function (result) {
        if (!result.data.success) {
            myDialogModal(result.data.errorMsg, 'Save error', 'OK', false)
                .result.then(function () {
                    $scope.validationdisabled = false;
                });
        } else {
            $scope.displayMessages('save', true);
        }
    });
}

Is this the correct way? What will happen to the promise of the first call? Also if the call for all is unsuccessful, it will display 10 popups with errors. Is there a more efficient way to do this?

0

There are 0 best solutions below