I have an array queue
that I push objects to it when they are modified. If the user presses save
, then I will loop through the queue
and apply the appropriate API call for them.
If the API call goes through successfully, I want to remove the item from the queue
, otherwise keep it inside and notify the user that some items were not successfully saved. I currently have this (in AngularJS)
var unsuccessfulItems = [];
var promise = queue.map(function(item) {
var defer = $q.defer();
myCallFunction( item
, function( response ) {} // Success
, function( response ) { // Error
unsuccessfulItems.push(item);
}
)
defer.resolve();
return defer.promise;
})
// Once all items have been processed
$q.all( promise ).then( function() {
queue = unsuccessfulItems;
});
Is there a better way of doing this?
See Question&Answers more detail:os