JavaScript Simplified · One concept, one day, one project · Day 26 of 32
setTimeout and Promises
Running code after a delay, and the modern way to represent a result that will arrive later. You build a food order that moves through its stages.
By the end of today you can
- Delay code with
setTimeout - Explain what a Promise is in plain words
- Handle a Promise with
.thenand.catch - Build a food order simulator
Yesterday you saw that some results arrive later. Today you learn the two tools for handling "later": setTimeout for a plain delay, and Promises, the modern way to represent a value that is on its way, which might succeed or fail.
1. setTimeout
Definition
setTimeout runs a function once, after a delay you give in milliseconds (1000 ms is one second). It does not block; the rest of your code keeps running.
2. Promises
Definition
A Promise is an object that represents a result that will be ready later. It is like a receipt: you do not have the food yet, but you have something that will become the food, or an explanation if something goes wrong. A Promise either resolves (succeeds, with a value) or rejects (fails, with a reason).
resolve is called on success, reject on failure. .then receives a resolved value; .catch receives a rejection. Here it is in stock, so .then runs after one second..then handles success, .catch handles failure. You chain them so your code reads as "when it arrives, do this; if it fails, do that". Every data request you make will have both.
Today’s Mini Project
🍔 Food Order Simulator
Model an order moving through its stages: placed, processing, then ready, using delays. Then wrap it in a Promise so it can succeed or fail.
Call placeOrder a second time with false for a sold-out item, and confirm the .catch message runs instead of .then.
Write a Promise that resolves with "Payment received" after 2 seconds, and log the result with .then.
Expected: "Payment received" appears two seconds later.
A Promise is just a receipt for a value that is coming. Once you see it that way, the fear leaves: you are handed something now that becomes the real result later, with a clear path for success and failure. Tomorrow you get an even cleaner way to use them.
Checkpoint
What is a Promise?
An object representing a result that will be ready later. It either resolves with a value (success) or rejects with a reason (failure). It is like a receipt for a future value.
What do resolve and reject do?
resolve finishes the Promise successfully with a value; reject finishes it with a failure reason. .then receives the resolved value, .catch receives the rejection.
What does the number in setTimeout mean?
The delay in milliseconds before the function runs. 1000 is one second.
Tick Before You Move On
- ☐ I can delay code with setTimeout
- ☐ I can explain what a Promise is
- ☐ I can handle a Promise with .then and .catch
- ☐ I built the Food Order Simulator
Quick Recap
setTimeout runs a function once after a delay, without blocking. A Promise represents a value that will arrive later. It resolves on success or rejects on failure. .then handles the success, .catch handles the failure.
Next: Day 27. Chaining .then works, but there is a cleaner way to write asynchronous code that reads almost like normal steps: async and await.