Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Promise.reject

Creates a promise that is already rejected with an error.

Available since: ES2015

Syntax

Promise.reject(reason)

Parameters

ParameterTypeDescription
reasonanyThe reason for rejection — usually an Error object.

Return value

PromiseAn already rejected promise.

Examples

A rejected promise and its handling:

try {
  await Promise.reject(new Error('Not found'));
} catch (error) {
  console.log(error.message);
}

Output:

Not found

Notes

In an async function writing throw error is simpler than return Promise.reject(error) — the result is the same.

See also

In the tutorial

  • Promises — Promises in JavaScript: promise states, then, catch, finally, creating a promise with new Promise, and chains.

Thanks for reading!

Donate to the site — we work hard for you.