×

javascript · level 3 · unit 5 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

waits for many promises at the same time.

Awaiting three requests one by one is : each waits for the last, so the waits add up. Start them all first, then wait for the lot with Promise.all. They run , so the total wait is only as long as the slowest one.

Promise.all takes an array of promises and resolves with an array of their values, in the order you gave them, whichever finishes first. If any one rejects, the whole thing rejects straight away with that error.

const base = "https://api.example.com/users";
async function getName(id) {
  const reply = await fetch(`${base}/${id}`);
  const user = await reply.json();
  return user.name;
}
const names = await Promise.all([1, 2, 3].map(getName));
console.log(names);

[1, 2, 3].map(getName) starts all three requests to the practice server at once and gives back an array of promises.