Last active
June 20, 2020 12:40
-
-
Save paulhhowells/cbafb4b0940f7c471f9251bf5ddcb4b1 to your computer and use it in GitHub Desktop.
Catching Errors
This file contains 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
// Catching Errors | |
/* | |
Run this in a terminal with: node catching-errors.js | |
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise | |
https://javascript.info/promise-error-handling | |
Promise executors and promise handlers have an 'invisible try…catch' around them. | |
If an exception happens, it is caught and treated as a rejection. | |
So reject are throw equivalent within `Promise((resolve, reject) => { … })` and `.then(() => { … })`. | |
However if you use them in a try…catch block then it is not quite the same, reject will exit the block and thus skip the catch block. | |
Code continues to run after reject & resolve so consider using a return. (Although a subsequent reject or resolve would be ineffective). | |
e.g. | |
return reject(error); | |
or | |
reject(error); | |
return; | |
*/ | |
[ | |
{ state: '1 succeeded:', success: true }, | |
{ state: '2 failed:', success: false }, | |
{ state: '3 other error:', success: null }, | |
].forEach(response => { | |
setTimeout(() => test(response), 1000); | |
}); | |
function test (response) { | |
const promise = new Promise((resolve, reject) => { | |
console.log(response.state); | |
try { | |
// Response might be something asynchronous such as provided by the .then from an API call. | |
if (response.success === true) { | |
resolve(response.state + ' STUFF'); | |
console.log('Runs after resolve!'); | |
return; | |
} else if (response.success === false) { | |
return reject(new Error(response.state + ' FAILED')); // Bypasses `catch (exception)` and is caught by `.catch(error =>`. | |
// console.log('Runs after reject!'); // Runs without a return above. | |
} else { | |
throw new Error(response.state + ' AN OTHER ERROR'); // Caught by `catch (exception)`. | |
// console.log('after throw'); // Will not run. | |
} | |
// Stuff would run here if resolve & return did not use or were not followed by a return; | |
} catch (exception) { | |
console.error('Catch:', exception); // Catch: Error: 3 other error: AN OTHER ERROR | |
// Need a throw or reject or .catch below will not see anything: | |
// throw new Error(exception); // equivalent to reject(exception) | |
reject(exception); // Send full exception instead of using reject() or reject(new Error(exception.message)); | |
} | |
}); | |
promise | |
.then(result => { | |
console.log('THEN:', result); | |
}) | |
.catch(error => { | |
console.error('ERROR:', error); | |
}) | |
.finally(() => { | |
console.log('FINALLY'); | |
}); | |
} | |
/* | |
// Add catch & finally to async await by using a try catch finally block. | |
try { | |
await thing(); | |
// throw new Error('oof!'); | |
} catch (error) { | |
console.error(error); | |
} finally { | |
console.log('finally'); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment