Unhandled promise rejection warning node js

This means that a promise you called rejected, but there was no catch used to handle the error. Add a catch after the offending then to handle this properly.

The origin of this error lies in the fact that each and every promise is expected to handle promise rejection i.e. have a .catch(…) . you can avoid the same by adding .catch(…) to a promise in the code as given below.

let done = true

const isItDoneYet = new Promise((resolve, reject) => {
  if (done) {
    const workDone = 'Here is the thing I built'
    resolve(workDone)
  } else {
    const why = 'Still working on something else'
    reject(why)
  }
})

isItDoneYet.then(res => console.log(res)).catch(err => console.log(err))

Leave a Reply