JavaScript · Middle · Question 20 of 75

How long will this code take if each request lasts 1 second?

for (const id of [1, 2, 3]) {
  await fetchUser(id);
}

Explanation

Each await waits for completion before the next iteration. If the requests are independent, run them in parallel: await Promise.all(ids.map(fetchUser)) - about 1 second. Sequential order is needed when the next step depends on the previous one.

Further reading

Problem with a question

JavaScript · Middle · question 20

Test yourself

Take the JavaScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz