Skip to content

Instantly share code, notes, and snippets.

@curioussavage
Last active September 2, 2015 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curioussavage/c7b4c7b2c0cad7b7767d to your computer and use it in GitHub Desktop.
Save curioussavage/c7b4c7b2c0cad7b7767d to your computer and use it in GitHub Desktop.
Just an a demonstration of error catching with promises.
function loggedFrom(from) {
return function(arg) {
console.log(from, arg);
}
}
var loggedFromCatch = loggedFrom('catch: ');
var loggedFromCallback = loggedFrom('second Callback: ');
function doSomething() {
return new Promise(function(resolve, reject) {
resolve('hello');
});
}
function doSomethingAndFail() {
return new Promise(function(resolve, reject) {
throw 'oh noes! orignal promise error'
resolve('hello');
});
}
// second callback not called
doSomething().then(function(val) {
throw 'error from 1st callback';
}, function(err) {
loggedFromCallback(err);
});
doSomething().then(function(val) {
throw 'error from 1st callback';
}).catch(function(err) {
loggedFromCatch(err);
return err;
});
doSomethingAndFail().then(function(val) {
// do stuff
}, function(err) {
loggedFromCallback(err);
});
doSomethingAndFail().then(function(val) {
// do stuff
}).catch(function(err) {
loggedFromCatch(err);
return err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment