Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 10.5 — Promises
JavaScript · Example 4 of 4 ES2024+

Promise.withResolvers: resolve from outside

withResolvers returns the promise together with its resolve and reject, handy when the code that settles it lives somewhere else, such as an event listener.

A promise is an object standing for a result that isn't ready yet: it starts pending and settles once, as fulfilled with a value or rejected with a reason, and you react with then, catch and finally.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write wait(ms, value) that returns a promise fulfilled with value after ms. Use it to log ready after 100 ms.

02

Write checkAge(age) that returns a promise rejected with Error("Too young") if age < 18 and fulfilled with "Welcome" otherwise. Call it with 20 and 12, logging both outcomes, and log checked after each with finally.

Explain it without notes

01

What are the three states of a promise, and what does "settled" mean?

02

When does the executor passed to new Promise run, and what happens if it throws?

03

What's the difference between then, catch and finally?

04

Why do promise callbacks run asynchronously even when the promise is already fulfilled?

Promise.withResolvers: resolve from outside ES2024+

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

User agreed