0

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?

7
  • 2
    Have you considered moving from promises to async/await? It will simplify your script and help the readability of your code. Simply put: you await for a method on the service and, after gettting the response, you continue with the next action. Commented Oct 7, 2019 at 21:43
  • No I haven't but will have a look. I assume there is documentation I can read on angular site. But wil there be documentation for angularjs 1.x ? Commented Oct 7, 2019 at 21:45
  • async/await is a native javascript feature which was introduced with ES2017 (ES8). To use it, you should ensure the javascript version of your angular app ponits to ES8 or higher. Commented Oct 7, 2019 at 21:54
  • @MiquelCanal The ES6 promises from async/await are not integrated with the AngularJS framework. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Commented Oct 7, 2019 at 22:55
  • 1
    What are you trying to do? The code as written will call the service 10 times with the same data. What is the point to that? Commented Oct 7, 2019 at 23:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.