Promise.resolve
Creates a promise that is already fulfilled with a value.
Available since: ES2015
Syntax
Promise.resolve(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value the promise is fulfilled with. |
Return value
PromiseAn already fulfilled promise.
Examples
A ready promise:
const promise = Promise.resolve('cached data');
console.log(await promise);Output:
cached dataA function that always returns a promise:
const cache = new Map([['anna', { name: 'Anna' }]]);
function getUser(id) {
if (cache.has(id)) return Promise.resolve(cache.get(id));
return new Promise((resolve) => setTimeout(() => resolve({ name: 'Loaded' }), 50));
}
console.log((await getUser('anna')).name);
console.log((await getUser('oleg')).name);Output:
Anna
LoadedNotes
If you pass a promise it is returned as is. Inside an async function Promise.resolve is unnecessary: any returned value is wrapped in a promise anyway.
See also
- Promise.reject — Creates a promise that is already rejected with an error.
- Promise — A promise of a result that will be ready later: success or failure.
In the tutorial
- Promises — Promises in JavaScript: promise states, then, catch, finally, creating a promise with new Promise, and chains.