Created
January 17, 2017 11:39
-
-
Save code-atom/e98e08b48c883af6f89c8b9df567a111 to your computer and use it in GitHub Desktop.
Understand how promise work and execute in javascript
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
app.run(function ($http, $q) { | |
var promise = $q.when('i m resolved'); | |
promise.then(function(value){ | |
console.log(value + " 1"); | |
}); | |
var innerPromise = promise.then(function(value){ | |
console.log(value + " 2"); | |
}) | |
var innerPromise2 = promise.then(function(value){ | |
console.log(value + " 3"); | |
}) | |
innerPromise2.then(function(){ | |
console.log('inner promise 2 first register then called'); | |
}); | |
innerPromise2.then(function(){ | |
console.log('inner promise 2 second register then called'); | |
}); | |
innerPromise.then(function(){ | |
console.log('inner promise 1 first register then called'); | |
}); | |
innerPromise.then(function(){ | |
console.log('inner promise 1 second register then called'); | |
}); | |
Promise.resolve() | |
.then( () => { | |
// Makes .then() return a rejected promise | |
throw 'Oh no!'; | |
}) | |
.catch( reason => { | |
console.error( 'onRejected function called: ', reason ); | |
}) | |
.then( () => { | |
console.log("I am always called even if the prior then's promise rejects"); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment