Command Palette

Search for a command to run...

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

Settles once, throws reject, callbacks are always async

All three promises were settled before the last line ran, yet their callbacks still waited until the synchronous code finished.

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?

Settles once, throws reject, callbacks are always async

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

Synchronous code ends here
Value: first value
Caught: thrown inside the executor
Already fulfilled, still async: 42