Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnhandledPromiseRejectionWarning on async await promise

Tags:

UnhandledPromiseRejectionWarning on async await promise

I have this code:

function foo() {
  return new Promise((resolve, reject) => {
    db.foo.findOne({}, (err, docs) => {
      if (err || !docs) return reject();
      return resolve();
    });
  });
}

async function foobar() {
  await foo() ? console.log("Have foo") : console.log("Not have foo");
}

foobar();

Which results with:

(node:14843) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): false

(node:14843) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Note: I know I can solve this issue like this:

foo().then(() => {}).catch(() => {});

But then we are "back" to callbacks async style.

How do we solve this issue?

like image 451
Raz Buchnik Avatar asked Oct 15 '18 06:10

Raz Buchnik


People also ask

Can we use async await in promise?

async and await Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

Which is better async await or promises?

It is just a wrapper to restyle code and make promises easier to read and use. It makes asynchronous code look more like synchronous/procedural code, which is easier to understand. await can only be used in async functions. It is used for calling an async function and waits for it to resolve or reject.

How do you handle reject In await?

Handling rejected promises You can handle rejected promises without a try block by chaining a catch() handler before awaiting the promise.


2 Answers

Wrap your code in try-catch block.

async function foobar() {
  try {
    await foo() ? console.log("Have foo") : console.log("Not have foo");
  }
  catch(e) {
    console.log('Catch an error: ', e)
  }
}
like image 157
Raghav Garg Avatar answered Oct 07 '22 03:10

Raghav Garg


then(() => {}).catch(() => {}) isn't needed because catch doesn't necessarily should go after then.

UnhandledPromiseRejectionWarning means that a promise weren't synchronously chained with catch, this resulted in unhandled rejection.

In async..await, errors should be caught with try..catch:

async function foobar() {
  try {
    await foo() ? console.log("Have foo") : console.log("Not have foo");
  } catch (error) {
    console.error(error);
  }
}

The alternative is to handle errors at top level. If foobar is application entry point and isn't supposed to be chained anywhere else, it's:

foobar().catch(console.error);

The problem with foo is that it doesn't provide meaningful errors. It preferably should be:

if (err || !docs) return reject(err);

Also, most popular callback-based libraries have promise counterparts to avoid new Promise. It mongoist for mongojs.

like image 41
Estus Flask Avatar answered Oct 07 '22 05:10

Estus Flask