Skip to content

Instantly share code, notes, and snippets.

@BideoWego
Created September 14, 2018 14:41
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 BideoWego/9e7f41e334912312761e8f72e33aff36 to your computer and use it in GitHub Desktop.
Save BideoWego/9e7f41e334912312761e8f72e33aff36 to your computer and use it in GitHub Desktop.
Error handling and stack traces in promise chain catches etc...
function eq(a, b) {
return new Promise(function(resolve, reject) {
a === b ?
resolve(`${ a } === ${ b }`) :
reject(`${ a } !== ${ b }`);
});
}
function eqs(a, b) {
return new Promise(function(resolve, reject) {
try {
if (a === b) {
resolve(`${ a } === ${ b }`);
} else {
throw new Error(`${ a } !== ${ b }`);
}
} catch(e) {
reject(e.stack);
}
});
}
Promise.resolve('Running with only reject')
.then(function(res) {
console.log(res);
return eq(2, 2);
})
.then(function(res) {
console.log(res);
return eq(3, 2);
})
.then(function() {
console.log("I'll never run :(");
})
.catch(function(err) {
console.error(err);
console.log();
return 'Running with Error stack trace';
})
.then(function(res) {
console.log(res);
return eqs(2, 2);
})
.then(function(res) {
console.log(res);
return eqs(3, 2);
})
.then(function() {
console.log("I'll never run either :(((");
})
.catch(function(err) {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment