+1 vote
in NodeJS Essentials by

What's wrong with the code snippet?

new Promise((resolve, reject) => {

  throw new Error('error')

}).then(console.log)

1 Answer

0 votes
by

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)

})

Related questions

+1 vote
asked May 30, 2020 in NodeJS Essentials by SakshiSharma
+1 vote
asked May 31, 2020 in NodeJS Essentials by Robindeniel
...