Skip to content

Instantly share code, notes, and snippets.

@agrublev
Created July 11, 2013 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrublev/5980333 to your computer and use it in GitHub Desktop.
Save agrublev/5980333 to your computer and use it in GitHub Desktop.
AngularJS using $q promises with $resource before Version 1.2 of Angular comes out
var errors = [];
var returnData = [];
angular.forEach(array,function(val,ind){
var tmpDef = $q.defer();
errors.push(tmpDef.promise);
some$resource.save({someId: val.id},'',function(data){
// success resolve promise with data returned
tmpDef.resolve('success');
returnData.push(data);
},function(data){
// error resolve promise with data returned
tmpDef.reject('error');
returnData.push(data);
});
});
$q.all(errors).then(function(data){
// All $resource calls are resolved this is the final data
console.log(returnData,data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment