Last active
November 9, 2016 14:29
-
-
Save EricLondon/09d6731387dbb54b7afa7424b5f6e5c4 to your computer and use it in GitHub Desktop.
NodeJS 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 error = undefined; | |
| var result = "wee!"; | |
| var chainableMethod = function () { | |
| return new Promise(function(resolve, reject) { | |
| if (error) { | |
| console.log('got error'); | |
| return reject(error); | |
| } | |
| console.log('no error'); | |
| return resolve(result); | |
| }); | |
| } | |
| var call_chainable = function() { | |
| chainableMethod() | |
| .then(function(res){ | |
| console.log(res); | |
| }) | |
| .catch(function(err) { | |
| console.log(err); | |
| }); | |
| } | |
| call_chainable(); | |
| error = "crap"; | |
| call_chainable(); | |
| // run: node app.js | |
| // output: | |
| // no error | |
| // got error | |
| // wee! | |
| // crap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment