Skip to the content

Waiting for several things

JavaScript · Unit 18: Asynchronous code

When one request doesn't depend on the other, waiting for them in line is time given away. Promise.all takes an array of promises, sets them all going and waits for them to finish.

It hands back an array of results in the same order as the array you gave it.

const api = "https://api.bybecode.dev";
const rs = await Promise.all([
  fetch(api + "/paquete/9"),
  fetch(api + "/paquete/7"),
]);
console.log(rs.length, rs[0].status);

Prints

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

    Parcel 9 answers faster than parcel 7. What does it print?

  2. 2. Predict the output

    Parcel 9 answers fast and parcel 7 takes longer. What does it print?

  3. 3. Complete the code

    Fill in to give each result its own name.

  4. 4. Predict the output

    The address /caida is the one that never answers. What does it print?

  5. 5. Find the bug

    It should print how many answers landed, that is 2. Which line has the error?

  6. 6. Complete the code

    Fill in statuses(paths), which hands back the status of every address.

  7. 7. Multiple choice

    The address of the second request is built from a value that comes in the first one. What should you do?

  8. 8. Predict the output

    Parcel 9 has 0 days left (dias) and parcel 7 has 2. What does it print?

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 →