Fulfilled or rejected: then, catch and finally
Order 1 skips catch; order 2 skips then. finally runs for both.
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.
Write wait(ms, value) that returns a promise fulfilled with value after ms. Use it to log ready after 100 ms.
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
What are the three states of a promise, and what does "settled" mean?
When does the executor passed to new Promise run, and what happens if it throws?
What's the difference between then, catch and finally?
Why do promise callbacks run asynchronously even when the promise is already fulfilled?
Preview
Console
Nothing logged yet.Expected console output
Order 1: eating a Margherita
Order 1: clear the table
Order 2 problem: Out of dough
Order 2: clear the table