sdxjs icon indicating copy to clipboard operation
sdxjs copied to clipboard

Section 3.7 mistake?

Open adecora opened this issue 7 months ago • 0 comments

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?

adecora avatar Jul 16 '24 20:07 adecora