Created
March 11, 2014 16:30
-
-
Save lotsofcode/9489410 to your computer and use it in GitHub Desktop.
angular promises resolve/reject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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