Created
May 21, 2015 14:32
-
-
Save riston/2b91e464d03700c399e2 to your computer and use it in GitHub Desktop.
Promise example
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 p1 = new Promise(function (resolve) { | |
return resolve(10); | |
}); | |
var add = function (num) { | |
return new Promise(function (resolve) { | |
return resolve(10 + num); | |
}); | |
}; | |
p1 | |
.then(function (result) { | |
console.log("First result", result); | |
return result; | |
}) | |
.then(function (prevResult) { | |
console.log("Prev", prevResult); | |
return add(prevResult); | |
}) | |
.then(function (final) { | |
console.log("Final result", final); | |
// Awesome throw error | |
throw Error("I Am awesome"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment