Skip to content

Promises - More About

The Promise constructor is not the only way to make a promise. This page covers the shortcuts for promises that are already settled, what happens when a then() callback returns another promise, and Promise.withResolvers(), which hands out the resolve and reject functions for use outside the executor.

Promise.resolve(value) returns a promise that is already fulfilled with value. It is useful when a function must return a promise but already has the answer, for example from a cache.

Even though the promise is fulfilled from the start, its then() callback does not run straight away. JavaScript queues it to run after the code that is currently running has finished:

const ready = Promise.resolve('cached value');
ready.then((value) => console.log('then() received:', value));
console.log('This line runs first');

This prints:

This line runs first
then() received: cached value

Because every then() callback runs later in this way, code that handles a promise behaves the same whether the promise settles at once or after a delay.

Promise.reject(reason) returns a promise that is already rejected with reason:

const failed = Promise.reject(new Error('Not signed in'));
failed.catch((error) => console.log('catch() received:', error.message));

This prints:

catch() received: Not signed in

Only the first call to resolve or reject counts, and later calls are ignored:

const promise = new Promise((resolve, reject) => {
resolve('first');
resolve('second');
reject(new Error('too late'));
});
promise.then((value) => console.log(value));

This prints:

first

When a then() callback returns a promise, the chain waits for that promise to settle and passes its value to the next step. This is how one slow step follows another without nesting:

const wait = (ms, value) => new Promise((resolve) => setTimeout(resolve, ms, value));
wait(100, 'user 7')
.then((user) => wait(100, `the to-do list for ${user}`))
.then((list) => console.log(`Loaded ${list}`));

This prints:

Loaded the to-do list for user 7

The second then() receives the string, not a promise, because promises never nest inside one another: a promise fulfilled with a promise takes on that promise’s result.

Promise.resolve() also accepts a promise, which it returns unchanged. That makes it a simple way to handle a function that sometimes returns a value and sometimes a promise:

const cache = new Map([['theme', 'dark']]);
const getSetting = (name) => {
if (cache.has(name)) {
return cache.get(name);
}
return new Promise((resolve) => setTimeout(resolve, 50, 'en-GB'));
};
Promise.resolve(getSetting('theme')).then((value) => console.log('theme:', value));
Promise.resolve(getSetting('language')).then((value) => console.log('language:', value));

This prints:

theme: dark
language: en-GB

It is still better to write functions that always return a promise, because their callers then need no such wrapping.

Sometimes the code that decides when a promise settles is not inside the executor, such as an event handler set up elsewhere. Promise.withResolvers(), added in ES2024, returns an object holding a new promise together with its resolve and reject functions:

const { promise, resolve } = Promise.withResolvers();
promise.then((message) => console.log(message));
setTimeout(() => resolve('The door has opened'), 50);
console.log('Waiting for the door');

This prints:

Waiting for the door
The door has opened

Older code does the same by assigning resolve and reject to outer variables from inside an executor. See Promise.withResolvers() on MDN for the details.

Steve Griffith covers this topic in his video More About JavaScript ES6 Promises methods.