3

I was wondering if there is any possibility to get a callback at the end of a series of requests. I'm attaching an example code so you can better understand :

for (var i = 0; i < $scope.array.length; i++) {
    factory.getX($scope.array[i].id)
        .success(function (_response) {
            /* call function if it's the last request in the for loop*/
        })
        .error(function (_err) {
            $scope.displayAlert(_err)
        })
}

In the factory object, I have a $http.get() function. Thanks !

1
  • 2
    take a look at $q.all Commented Mar 17, 2015 at 9:31

2 Answers 2

3

If you want to have a callback when all the requests are completed you could save all the promises that are created in a array and make a common promise with var allRequests = $q.all(arrayOfPromises)

You could then do allRequests.then(callback) to implement your callback

Sign up to request clarification or add additional context in comments.

1 Comment

can you, please, be more explicit on saving all the promises in an array ?
1

Thank you all, I've solved the problem like this :

 var promises = $scope.array.map(function (item) {
                        return factory.getX(item.id)
                            .success(function (_response) {

                            })
                            .error(function (_err) {
                                $scope.displayAlert(_err)
                            })
                    })

 $q.all(promises).then(function () {
        console.log("Solved all the promises")
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.