sdxjs
sdxjs copied to clipboard
Section 3.7 mistake?
Hi, on the section about how to handle errors with asynchronous code on the solutions presented is to be consistent and always return something.
async function returnImmediately () {
try {
return Promise.reject(new Error('deliberate'))
} catch (err) {
return new Error('caught exception')
}
}
const result = returnImmediately()
result.catch(err => console.log(`caller caught ${err}`))
The catch block is just returning the error, but in this way the returning error from the catch will be only handle by the .then.
It would be not better to return a Promise(new Error('caught exception'))
as in the then block or just throwing the error return new Error('caught exception')
so in this manner it will be handle by the result
.catch?