Promises and await
You'll learn why some results arrive later, what a promise is, and how await waits for one without freezing the app.
await.Watch & read
Start with the required ones. The others are there when you need them.
See it step by step
Watch await pause one function while the rest of the program carries on.
Explanation
Some jobs take time. Asking a server for Dhaka's weather may take a second. If JavaScript stood still and waited, the whole app would freeze: no scrolling, no tapping. So JavaScript starts the job, carries on with other work, and comes back when the result is ready. Code that finishes later like this is called asynchronous.
Think of ordering food at a restaurant. You get a token with a number. You don't stand at the counter; you sit down and talk. When your number is called, you collect the food. The token is a promise of food.
The simplest way to see "later" is setTimeout. It runs a function after a delay:
console.log("order placed");setTimeout(() => console.log("food ready"), 2000);console.log("talking with friends");// order placed, talking with friends, food ready
A promise is that token in code: an object that says "a value is coming". While it waits it is
pending. Later it is either fulfilled with a value, or it fails with an error. fetch, which you'll
meet next, gives back a promise.
To get the value out, write await in front of the promise, inside a function marked async:
async function showTemp() {console.log("Loading...");const temp = await getTemp("Dhaka");console.log("Dhaka:", temp);}
await pauses only this function. The rest of the program keeps going: taps, scrolling, other
code. When the value arrives, the function continues from the await line, with temp filled in.
People often forget await. Then temp is not a number but the token itself, and the console prints
Promise { <pending> }. If you see that, look for a missing await. Also, await only works inside an
async function (or at the top of a module).
When you read async code, number the lines in the order they will run. Code before await runs now.
Code after await runs later, after everything else that is ready to go.
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1console.log("1. order placed");2setTimeout(() => {3 console.log("2. food ready");4}, 1000);5console.log("3. watch cricket");
1async function getScore() {2 console.log("A");3 const score = await Promise.resolve(250);4 console.log("C", score);5}6getScore();7console.log("B");
Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3
console.log("A"); setTimeout(() => console.log("B"), 0); console.log("C");In which order do they print? - 2 / 3
const temp = getTemp("Dhaka");prints asPromise { <pending> }. What is missing? - 3 / 3When a function reaches
await, what pauses?
Explain it back
How confident are you with Promises and await?
Done
setTimeout, async and await, and say in which order the lines print.