Skip to the content

async and await

JavaScript · Unit 18: Asynchronous code

The chain of then works, but it gets long. await does the same and reads top to bottom: it waits for the promise to finish and hands you its value like any other piece of data.

In the app you can use await on any line, because your code already runs inside an async function.

const api = "https://api.bybecode.dev";
const r = await fetch(api + "/clima");
const d = await r.json();
console.log(d.ciudad, d.grados);

Prints

Puebla 21

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. 1. Predict the output

    What does this code print?

  2. 2. Predict the output

    What does this code print?

  3. 3. Find the bug

    It should print the city from the weather. Which line has the error?

  4. 4. Complete the code

    Fill in degrees(path), which hands back the degrees at that address.

  5. 5. Predict the output

    What does this code print?

  6. 6. Put the lines in order

    Put it in order to print how many days parcel 9 has left (dias).

  7. 7. Multiple choice

    These two requests don't depend on each other. How do they go out?

  8. 8. Find the case that fails

    The practice only has weather at /clima and /clima/oaxaca; any other address answers that it doesn't exist. degrees(path) should give the degrees at that address, or 0 if there's no weather there. Which call does it fail with?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →