In JavaScript, async functions always return a promise object. To obtain the underlying value of the promise, use the await operator or call the then method. Attempting to use a promise object instead of its underlying value can lead to unexpected behavior.

Use the await operator to get the value contained in the promise. Alternatively, call then on the promise and use the value passed to the callback.

In the following example, the getData function returns a promise, and the caller checks if the returned promise is null:

However, the null check does not work as expected. The return null statement on line 2 actually returns a promise containing the null value. Since the promise object itself is not equal to null, the error check is bypassed.

The issue can be corrected by inserting await before the promise:

  • MDN: Using promises
  • MDN: Async functions
  • MDN: Await operator