This course shows you how to port asyncio code to JavaScript: async functions without
asyncio.run(), the promises behind await, awaiting in loops and all at once, timeouts, retries,
and promises that fail while nobody’s waiting for them.
Your project is to port a library’s morning holds check, counting reserved books at stand-in
kiosks, so everything runs offline. Here’s a first taste. The library’s Python asks three kiosks at once with
asyncio.gather():
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 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")
if __name__ == "__main__":
asyncio.run(main())
It prints the total, 29 holds waiting:
Total: 29 holds waiting
Checked 3 kiosks in 0.9 s
Port it line by line, and Python’s star becomes three dots, JavaScript’s spread. Here’s the port,
branch-status.js:
import { setTimeout as sleep } from "node:timers/promises";
const KIOSKS = {
Northgate: { delay: 120, holds: 14 },
Eastfield: { delay: 250, holds: 9 },
"Hollins Green": { delay: 900, holds: 6 },
};
const BRANCHES = Object.keys(KIOSKS);
async function checkKiosk(branch) {
const kiosk = KIOSKS[branch];
await sleep(kiosk.delay);
return { branch, holds: kiosk.holds };
}
const start = performance.now();
const kiosks = await Promise.all(
...BRANCHES.map(checkKiosk),
);
const total = kiosks.reduce(
(sum, kiosk) => sum + kiosk.holds,
0,
);
console.log(`Total: ${total} holds waiting`);
const elapsed = ((performance.now() - start) / 1000).toFixed(1);
console.log(`Checked ${kiosks.length} kiosks in ${elapsed} s`);
When you run it with node branch-status.js, JavaScript stops before a single kiosk answers:
TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
The TypeError says an object isn’t iterable, as Python’s own would. It stops at the
Promise.all() call on line 17, where the dots hand the checks over as separate arguments.
JavaScript’s async and await look so much like Python’s that a line-by-line port looks right.
Where it breaks is where JavaScript and asyncio differ, and the course takes each break in turn.
Ashdown Libraries’ four branches, Northgate, Eastfield, Hollins Green and Quarry Hill, each have a holds kiosk, where readers collect the books they reserved. Every morning, Owen, the systems librarian, runs a Python script that asks every kiosk at once, gives up after half a second, asks a failing kiosk again, and prints a board with the total and yesterday’s. The staff dashboard is moving to JavaScript, so you’ll port the script.
In the course, a function stands in for the kiosks, so every example runs offline.
By the end of this course, you’ll be able to:
- Port
async defandawaitwithoutasyncio.run(), and read the promise an async call returns - Await kiosks in turn with
for...of, all at once withPromise.all(), and keep every outcome withPromise.allSettled() - Give up on slow work with
Promise.race(), retry withtry/catcharoundawait, and handle a promise you await later - Port the whole holds check to JavaScript
You only need to know how to write Python asyncio code with async def, await, asyncio.run(),
gather(), sleep() and wait_for(). In JavaScript, you only need to know how to write
functions, arrow functions, array methods, modules and try/catch, and how to read files with
await readFile().
A current version of Node.js and of Python is fine: the examples use Node 26 and Python 3.15.
The course mixes short videos and text lessons, with exercises where you write the code yourself. Every video has a summary below it, and a quiz and a summary close the course.
Next up: Porting async def Without asyncio.run(), a text lesson.