Last active
October 16, 2015 00:04
-
-
Save stevenvachon/560451055e4b88b563d9 to your computer and use it in GitHub Desktop.
easy way to understand javascript promises
This file contains 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
"use strict"; | |
var p1 = new Promise( function(resolve) { | |
resolve(); | |
}); | |
var p2 = new Promise( function(resolve) { | |
resolve(); | |
}); | |
var p3 = new Promise( function(resolve) { | |
resolve(); | |
}); | |
var p4 = Promise.resolve().then( function() { | |
console.log(1); | |
return p1.then( function() { | |
console.log(2); | |
}); | |
}).then( function() { | |
console.log(3); | |
return p2.then( function() { | |
console.log(4); | |
}); | |
}).then( function() { | |
console.log(5); | |
return p3; | |
}).then( function() { | |
console.log(6); | |
throw new Error(); | |
}).then( function() { | |
// Skipped | |
console.log(7); | |
}).catch( function(error) { | |
console.log(error); | |
}).then( function() { | |
// Not skipped | |
console.log(8); | |
return 9; | |
}); | |
Promise.resolve(p4).then( function(value) { | |
console.log(value); //-> 9 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment