Owen’s morning check is built on one coroutine function, check_kiosk(), which asks a branch’s holds kiosk how many reserved books are waiting. It’s the first piece of branch_status.py to move to JavaScript. Its async and await make the trip almost unchanged, but the asyncio machinery around them stays behind.
Write async function Where Python Writes async def
Here’s the Python, cut down to one check. Each kiosk is an offline stand-in that answers after a set delay. Quarry Hill’s new kiosk isn’t on the list yet:
import asyncio
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():
kiosk = await check_kiosk("Northgate")
print(f"{kiosk['branch']}: {kiosk['holds']} holds waiting")
if __name__ == "__main__":
asyncio.run(main())
Python prints Northgate: 14 holds waiting after about a tenth of a second. Here’s the port, branch-status.js, in a folder whose package.json holds { "type": "module" }:
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 },
};
async function checkKiosk(branch) {
const kiosk = KIOSKS[branch];
await sleep(kiosk.delay);
return { branch, holds: kiosk.holds };
}
const kiosk = await checkKiosk("Northgate");
console.log(`${kiosk.branch}: ${kiosk.holds} holds waiting`);
Run node branch-status.js, and JavaScript prints the same line after the same pause:
Northgate: 14 holds waiting
The function body maps line for line, but the pause and the bottom of the file change. The port moves the body of main() to the top level of the module, and the last two rows also show the form that keeps a main():
| Python | JavaScript |
|---|---|
async def check_kiosk(branch): |
async function checkKiosk(branch) { |
| No async lambda | const checkKiosk = async (branch) => { ... }; |
await asyncio.sleep(0.12) |
await sleep(120); |
asyncio.run(main()) |
await at the top level, or await main(); |
if __name__ == "__main__": |
if (import.meta.main), with a main() |
Inside an async function, await works as in Python. It waits for the result and hands it over, pausing only its own function. The return line uses object shorthand, so branch stands for branch: branch.
Drop asyncio.run() and Await at the Top Level
Python needs asyncio.run() because nothing runs a coroutine until you start an event loop. JavaScript’s event loop is already running when your file starts, so a module can await at its top level, as you did with await readFile(). In a Python file, await outside a function is a SyntaxError.
Anywhere else, await belongs inside an async function, as in Python. While the module waits, its sleep() timer keeps Node alive. How the loop schedules that work comes later.
With a main() function, as in Owen’s script, end the file with if (import.meta.main) await main();. Here import.meta.main is true when you run the file directly.
Pause With sleep() in Milliseconds
The awaitable pause comes from setTimeout() in node:timers/promises, renamed to sleep on import, like Python’s from ... import ... as .... It counts milliseconds, like every JavaScript timer, so Northgate’s 0.12 becomes 120.
Three slips catch Pythonistas here, each with its fix:
await setTimeout(120)uses the global timer, which expects a callback, so it throws aTypeError. Importsleepinstead.await sleep(0.12)keeps Python’s seconds and waits about a millisecond. Give it120.awaitinside a plain function, such as an arrow passed to.forEach(), isSyntaxError: Unexpected reserved word. The outerasyncdoesn’t cover it, because each function is async or not on its own. A nesteddeforlambdain Python follows the same rule, but JavaScript passes callbacks where Python writes comprehensions, so you’ll encounter this restriction more often. Keep theawaitin the async function’s own body.
A JavaScript developer writes it the way your port does, with await inside an async function, sleep() given milliseconds, and no asyncio.run().
What does checkKiosk() hand back if you call it without await? In the next lesson, Seeing the Promise Behind an Async Call, you’ll call it on its own, log what comes back, and see what await has been waiting for.