JavaScript Reference ยท Promises and Async
Promise.resolve() and Promise.reject()
Reference for Promise.resolve() and Promise.reject(): create already-settled promises, convert values and thenables, and know when resolve returns the same promise.
Promise.resolve(value) returns a promise that is resolved with value. If value is already a genuine promise, you get that same promise back; if it is a thenable (an object with a then() method), the result follows it; otherwise the result is fulfilled with the value.
Promise.reject(reason) returns a promise that is rejected with reason. It never unwraps promises: the reason is used as it is.
const ready = Promise.resolve("hello"); const broken = Promise.reject(new Error("nope"));
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Promise.resolve(value) Promise.reject(reason)
| Name | Description |
|---|---|
value |
Any value, promise or thenable. |
reason |
The rejection reason, usually an Error. |
Return value: a promise. Promise.resolve() returns its argument unchanged if it is a native promise created by the same constructor; otherwise a new promise. Promise.reject() always returns a new rejected promise.
Exceptions: neither method throws for ordinary arguments.
Usage notes
- Handlers still run asynchronously. Even an already-settled promise delivers its result as a microtask, after the current synchronous code.
- Normalizing.
Promise.resolve(x)is a safe way to treat a value that may or may not be a promise the same way. - Starting a chain.
Promise.resolve().then(...)is a common way to begin a promise chain or to defer work until the microtask queue. - Do not wrap
reject()intry...catch. It does not throw; it returns a rejected promise that you must handle. - Unhandled rejections. A rejected promise without a handler triggers an
unhandledrejectionevent. - Tests and stubs. These methods are the shortest way to fake async functions:
() => Promise.resolve(data). - An
asyncfunction that returns a value or throws already makes the same promises for you.
Browser support
Both methods are supported in every current browser and Node.js.
Related
- Promise, then(), catch()
- Promise.withResolvers()
- Lesson: Promises