Awaiting Each Kiosk in Turn

Your port checks only Northgate so far. Owen’s morning board needs the total number of holds across all branches and the time it took to check them. In Python, that’s one comprehension inside main(), and its natural JavaScript port runs without an error. Whether its numbers are right is another matter.

Port the Comprehension to .map()

Owen’s script collects kiosk records with an async comprehension. Quarry Hill’s new kiosk isn’t on the list yet:

Language: Python
import asyncio
import time

KIOSKS = {
    "Northgate": {"delay": 0.12, "holds": 14},
    "Eastfield": {"delay": 0.25, "holds": 9},
    "Hollins Green": {"delay": 0.9, "holds": 6},
}

async def check_kiosk(branch):
    kiosk = KIOSKS[branch]
    await asyncio.sleep(kiosk["delay"])
    return {"branch": branch, "holds": kiosk["holds"]}

async def main():
    start = time.perf_counter()
    kiosks = [await check_kiosk(branch) for branch 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")

if __name__ == "__main__":
    asyncio.run(main())

The script prints Total: 29 holds waiting and Checked 3 kiosks in 1.3 s, with the elapsed time reflecting the three delays added up. Here’s how main() maps across:

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

Become a Member to join the conversation.