Created
September 14, 2018 14:41
-
-
Save BideoWego/9e7f41e334912312761e8f72e33aff36 to your computer and use it in GitHub Desktop.
Error handling and stack traces in promise chain catches etc...
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
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