Here is an array of IDs:
let idsArray = [1, 2, 3, 4, 5];
How can I ensure that a promise is returned only after all calls made within the loop are completed?
let deferredPromise = $q.defer(),
finalResult = [];
for (let i = 0; i < idsArray.length; i++) {
let id = idsArray[i];
Tools.remove(id).then(function(response) {
finalResult.push(response.id);
}).catch(function(errorResponse) {
finalResult.push(errorResponse)
});
}
deferredPromise.resolve(finalResult);
return deferredPromise.promise;