Categories

May 30 in Node.Js

Q:

What's wrong with the code snippet?

new Promise((resolve, reject) => {

  throw new Error('error')

}).then(console.log)

1 Answer

May 30

As there is no catch after the then. This way the error will be a silent one, there will be no indication of an error thrown.

To fix it, you can do the following:

new Promise((resolve, reject) => {

  throw new Error('error')

}).then(console.log).catch(console.error)

If you have to debug a huge codebase, and you don't know which Promise can potentially hide an issue, you can use the unhandledRejection hook. It will print out all unhandled Promise rejections.

process.on('unhandledRejection', (err) => {

  console.log(err)

})

Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

Madanswer
May 30 in Node.Js
May 31 in Node.Js
May 31 in Node.Js
...