Skip to content

Instantly share code, notes, and snippets.

@lotsofcode
Created March 11, 2014 16:30
Show Gist options
  • Select an option

  • Save lotsofcode/9489410 to your computer and use it in GitHub Desktop.

Select an option

Save lotsofcode/9489410 to your computer and use it in GitHub Desktop.
angular promises resolve/reject
var $injector = angular.injector(['ng']);
var $q = $injector.get('$q');
function promise(msg) {
var defer = new $q.defer();
defer.promise.then(function() {
console.log(msg + "I promised!");
}).then(function() {
console.log(msg + "I would do this too")
}).then(function() {
console.log(msg + "... and I would do this too")
}).catch(function() {
console.log(msg + "We've caught an error")
}).finally(function() {
console.log(msg + "After all is said and done")
});
return defer;
};
var defer_resolved = promise(" [resolved] ");
var defer_rejected = promise(" [rejected] ");
window.setTimeout(function() {
defer_resolved.resolve("I have been resolved!");
}, 0);
window.setTimeout(function() {
defer_rejected.reject("I have been rejected!");
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment