Skip to content

Instantly share code, notes, and snippets.

@timfulmer
Last active August 21, 2017 21:09
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 timfulmer/e0f8de74298e296eedad813ba8beda9d to your computer and use it in GitHub Desktop.
Save timfulmer/e0f8de74298e296eedad813ba8beda9d to your computer and use it in GitHub Desktop.
Promises and error handling
'use strict';
function promiseThatThrows(){
return new Promise((resolve,reject) => {
throw new Error('!!This needs to be handled!!');
});
}
function correctNestedErrorHandling(){
return new Promise((resolve,reject) => {
return promiseThatThrows()
.catch((err) => {
reject(err);
});
});
}
function hideError1(){
return new Promise((resolve,reject) => {
return promiseThatThrows();
});
}
function hideError2(){
return new Promise((resolve,reject) => {
return promiseThatThrows()
.catch((err) => {
throw err;
});
});
}
hideError1()
.catch((err) => {
console.log('This is not caught!!',err);
});
hideError2()
.catch((err) => {
console.log('This is not caught!!',err);
});
correctNestedErrorHandling()
.catch((err) => {
console.log('Correctly handled error',err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment