Skip to content

Instantly share code, notes, and snippets.

@EricLondon
Last active November 9, 2016 14:29
Show Gist options
  • Select an option

  • Save EricLondon/09d6731387dbb54b7afa7424b5f6e5c4 to your computer and use it in GitHub Desktop.

Select an option

Save EricLondon/09d6731387dbb54b7afa7424b5f6e5c4 to your computer and use it in GitHub Desktop.
NodeJS Promise Example
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