Promise.reject
Creates a promise that is already rejected with an error.
Available since: ES2015
Syntax
Promise.reject(reason)Parameters
| Parameter | Type | Description |
|---|---|---|
| reason | any | The 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 foundNotes
In an async function writing throw error is simpler than return Promise.reject(error) — the result is the same.
See also
- Promise.resolve — Creates a promise that is already fulfilled with a value.
- Promise.prototype.catch — Handles a promise's error.
- Error — The base error class: created and thrown with throw.
In the tutorial
- Promises — Promises in JavaScript: promise states, then, catch, finally, creating a promise with new Promise, and chains.