Lesson 5 of 8
Parallel requests
- 13 min
- 4 checks
- 3 hints
- Go to task
Goal
The slowest request's time, not the total
Explanation
You have three requests, and none of them needs another one's result, yet most of us write them as await after await. Each line then waits for the one before it, and the total time becomes the sum of all their times instead of just the longest one. The most common form of this problem is an await inside a for loop.
With Promise.all, you start all the requests together and wait for them just once, and it returns the results in the order of the requests, not the order they arrived in.
Note that Promise.all fails as a whole if a single request fails, so if partial results are good enough, use Promise.allSettled. A request that needs another request's result still goes after it with await, because running requests in parallel only works when they're independent.
Your task
Rewrite loadAll(names) so it starts all the requests together and returns their results in the order of names. Click the button before and after your change to compare the time.
Checks
4 checks- All the results are there, in request order
- All the requests start at once, not one after another
- It takes as long as the slowest request, not all of them combined
- It works with any list in any order