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 !
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
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With