Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Waiting for Every Kiosk With Promise.all()

Waiting for Every Kiosk With Promise.all()

Both versions, Python and your for...of port, ask the kiosks one after another, so both take 1.3 seconds. The three kiosks don’t depend on each other, so in Python you’d ask them all at once. Inside main(), asyncio.gather() takes a check for each branch and waits for all of them:

Language: Python
async def main():
    start = time.perf_counter()
    kiosks = await asyncio.gather(
        *(check_kiosk(b) for b in KIOSKS)
    )
    total = sum(kiosk["holds"] for kiosk in kiosks)
    print(f"Total: {total} holds waiting")
    elapsed = time.perf_counter() - start
    print(f"Checked {len(kiosks)} kiosks in {elapsed:.1f} s")

It prints Total: 29 holds waiting and Checked 3 kiosks in 0.9 s.

In JavaScript, gather()’s job belongs to Promise.all(). The .map() call gives back an array of promises, one for each kiosk check. That is what Promise.all() needs. JavaScript’s spread, three dots, unpacks an array into separate arguments, as the star does in a Python call. With the same stand-in as before, the line-by-line port is:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Both versions ask the kiosks one after another, so both take 1.3 seconds. The three kiosks don’t depend on each other, so in Python you’d ask them all at once.

00:12 Inside main(), asyncio’s gather() takes a check for each branch, and waits for all of them. The board prints the same total, twenty-nine holds, in 0.9 seconds.

00:25 In this lesson, you’ll port that wait to JavaScript.

00:31 The port keeps the clock and the top-level await from before, and gather()’s job belongs to Promise.all(), which waits for a group of promises.

00:41 Python’s star unpacks the generator’s checks into the call. JavaScript’s .map() hands each branch to checkKiosk(), passed by name, and gives back an array of promises.

00:52 That’s why the last board’s total read NaN, not a number. This time, promises are what you want. And JavaScript’s spread, three dots, unpacks an array into separate arguments, as the star does.

01:10 Run the port, with Python’s board beside it. What does the JavaScript port print? Your turn: pause the video and predict.

01:24 No board at all. JavaScript stops with a TypeError: object is not iterable. The stack’s first line points at Promise.all() itself.

01:36 It’s the error from the start of the course, and now you’ll see why.

01:42 Python’s gather() takes each awaitable as its own argument, and the star hands it three. Promise.all() takes one argument: an array of promises, or any other iterable.

01:54 The spread hands it three promises, so it reads Northgate’s promise as that array, and ignores the other two. A promise isn’t iterable, so Promise.all() rejects, and the await stops the script.

02:10 The part in parentheses names the hook a loop looks for, like Python’s __iter__(): that’s all you need about it for now. Listing the calls as separate arguments gives the same error.

02:23 So hand Promise.all() the array itself, and the three dots go. .map() builds the array of three promises, and Promise.all() gets it in one piece.

02:35 Run it, and the board matches Python’s: twenty-nine holds, in 0.9 seconds. That’s the time the slowest kiosk took to answer.

02:47 Why 0.9 seconds, and not 1.3? Calling an async function starts its work right away and hands back a promise. So .map() is where the checks start: all three set off together.

03:02 In Python, calling check_kiosk() only makes a coroutine; gather() wraps each in a task and starts it. In JavaScript, they’re running before Promise.all() sees them.

03:15 Promise.all() starts nothing: it waits until Hollins Green, the slowest, answers. The results come back in the array’s order, Northgate first, not in the order the kiosks answer, as with gather() in Python.

03:31 When you want each result by its own name, say the two busiest branches, Python unpacks the list that gather() returns. Two checks go in as two arguments, and two names take the results.

03:45 In JavaScript, the two calls go inside square brackets: one array literal. Square brackets on the left of the equals sign destructure the array into names by position, as Python’s unpacking does, but a missing value becomes undefined, not an error.

04:03 Run it, and Northgate and Eastfield have twenty-three holds between them.

04:09 When checks don’t depend on each other, start them together and wait once. Build the array of promises with .map(), hand the whole array to Promise.all(), and read the results by position or by name.

04:22 As with gather(), if one promise rejects, Promise.all() rejects right away. Next up, Reporting Failures With Promise.allSettled(): Quarry Hill’s new kiosk joins the list, and it isn’t answering yet.

Become a Member to join the conversation.