Need resolve outside the Promise? Promise.withResolvers in one line

Promise.withResolvers() gives you a promise with its resolve and reject in one object, instead of declaring two let variables and filling them inside new Promise. Useful when an event or a callback outside the function settles it.

Published 3 min read#Web development#Developer tools

Every JavaScript project has these two lines somewhere: let resolve, reject; and then new Promise only to pull the two functions out of it. A Promise hands you the button that settles it inside its own box, and you need it outside: on a button on the page, in an event, or in a callback that comes later. This article shows what Promise.withResolvers() does, when to use it, and how to write a confirm dialog with it that waits for the user.

What is Promise.withResolvers()?

Promise.withResolvers() is a static method that returns an object holding a new promise, together with the two functions that settle it: resolve and reject. You call it with no arguments.

According to the Promise.withResolvers() page on MDN, the two functions are the same ones passed to the executor of new Promise(). The page marks it Baseline, "Widely available", which means it works across many devices and browser versions.

For a business owner: it's like giving the customer an order number, and leaving the remote that says "ready" with the cook in the kitchen, not with the cashier.

What's wrong with the old way?

The old way works, but it forces you to declare two empty variables with let and fill them inside a function that runs right away, only so you can reach the two functions from outside.

This is how it looked:

let resolve, reject;
const promise = new Promise((res, rej) => {
  resolve = res;
  reject = rej;
});

Three small problems add up: the two let variables can be reassigned by mistake, the reader has to work out why there's an almost empty function inside the constructor, and anyone who copies the pattern repeats the same five lines in every file.

How do you write it with withResolvers?

With withResolvers the five lines become one, and you take only what you need from the object:

const { promise, resolve, reject } = Promise.withResolvers();

The MDN page itself says it is "exactly equivalent" to the old pattern, the difference being that it is "more concise and does not require the use of let". So there's no new behavior to learn: the same promise, the same two functions, in const variables.

Where does it help in a real project?

Promise.withResolvers() helps when the thing that settles the promise happens outside the function that created it: a user click, an event from a library, or an answer that arrives on a callback.

An example you'll find in any dashboard: a "Delete project" button opens a confirm dialog, and the code has to wait for what the user clicks. With withResolvers the dialog becomes a function that returns an answer:

function confirmDialog(dialog) {
  const { promise, resolve } = Promise.withResolvers();

  dialog.querySelector('.confirm').addEventListener('click', () => resolve(true), { once: true });
  dialog.querySelector('.cancel').addEventListener('click', () => resolve(false), { once: true });
  dialog.showModal();

  return promise;
}

deleteButton.addEventListener('click', async () => {
  if (await confirmDialog(deleteDialog)) {
    await deleteProject(projectId);
  }
});

There's no reject here, because the user always picks one of the two. And every button in the dialog calls resolve, so the waiting code carries on whatever the choice.

The same page has another example of the same kind: it turns a stream into an async iterable, and creates a new promise with withResolvers each time the stream is ready to read.

Where do you start today?

Search your project for let resolve or for a new Promise((res on its own inside a function. Every place you find is a candidate.

  1. Replace the lines with const { promise, resolve, reject } = Promise.withResolvers();, and drop whatever you don't use from the object.

  2. Make sure every path in the code ends in resolve or reject, like the two buttons in the example.

  3. If the event might never come, set a limit on the wait. We wrote about that for requests in fetch timeouts with AbortSignal.

And if you like these small additions that shorten code you write every day, see also grouping arrays with Object.groupBy, and the rest of our web development articles.

Where in your code is there a let resolve still waiting to be deleted?

FAQ

Does withResolvers behave differently from new Promise?

No. The MDN documentation says it is exactly equivalent to creating a new Promise and keeping resolve and reject in two variables, only more concise and without let.

Can I use it in browsers today?

Yes. The MDN page marks it Baseline, Widely available, which means it works across many devices and browser versions.

Do I have to use reject if no error is possible?

No. You take only what you need from the object, like const { promise, resolve } = Promise.withResolvers(), and leave reject out.

Share

More articles