Promises
JavaScript · Unit 18: Asynchronous code
- Full plan
- JavaScript
- 10 exercises
A setTimeout lets you know, but it hands you no data. For data that is on its way there's something better: a promise. A promise is a value that isn't there yet, with a then to say what to do when it lands.
fetch(address) asks for data and returns a promise. In the app the data is practice data: fetch answers a few fixed addresses the engine carries, always the same. That practice server is Mexican, so its paths and its keys are in Spanish: /clima is the weather and grados is the degrees.
const api = "https://api.bybecode.dev";
fetch(api + "/clima").then((r) => {
console.log(r.status);
});
console.log("request sent");Prints
request sent 200
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Predict the output
The prices are
{ cafe: 45, pan: 12, leche: 28 }. What does it print?3. Predict the output
The weather answers
{ ciudad: 'Puebla', grados: 21, llueve: false }. What does it print?4. Complete the code
Fill in to print the city from the weather.
5. Predict the output
What does this code print?
6. Predict the output
The address
/secretoanswers with status 401, which means "no permission". What does it print?7. Find the bug
It should print the object with the prices. Which line has the error?
8. Put the lines in order
Put it in order to print how many days parcel 9 has left (
dias).9. Predict the output
Parcel 9 is already delivered and answers fast; parcel 7 is on the way and takes longer. What does it print?
10. Multiple choice
What does this code do?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.