You can use $q.all to achieve the multiple promise calls.
HTML code :
<div ng-app>
<div ng-controller="PromiseCtrl">
{{result}}
</div>
</div>
Controller function :
function PromiseCtrl($scope, $q, $timeout) {
// Our promise function with its delay as argument
var getPromise = function(delay) {
// Creates a Deferred object
var deferred = $q.defer();
$timeout(function() {
// Resolve the promise at the end of the delay if said delay was > 0
if(delay > 0) {
deferred.resolve("Success");
} else {
deferred.reject("Fail");
}
}, delay);
// The promise of the deferred task
return deferred.promise;
};
// Init
$scope.result = "Waiting";
/*
* Combines multiple promises into a single promise
* that will be resolved when all of the input promises are resolved
*/
$q.all([
getPromise(1000),
getPromise(2000),
getPromise(3000) // <--- Try something less than 0
]).then(function(value) {
// Success callback where value is an array containing the success values
$scope.result = value;
}, function(reason) {
// Error callback where reason is the value of the first rejected promise
$scope.result = reason;
});
}
Please find the plnkr : https://jsfiddle.net/samirshah1187/Lpach13a/
$httpin your controller?ids and for each you want to make a call to your REST endpoint and have all values returned at once so you can store the results in some value? If that is the case, you should look upPromise.all- developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…