Owen’s morning check starts with one small coroutine. check_kiosk() asks a branch’s holds kiosk how many reserved books are waiting, and everything else in branch_status.py is built on it. The new staff dashboard is written in JavaScript, so the script is moving over, and check_kiosk() goes first.
Python’s async and await both make the trip almost unchanged. The asyncio machinery around them stays behind. In this lesson, you’ll port check_kiosk(), run it without asyncio.run(), and pause it the JavaScript way.
Write async function Where Python Writes async def
Here’s the Python, cut down to one check. The kiosks are stand-ins that answer after a set delay, so the script runs offline. 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())
After about a tenth of a second, Python prints Northgate: 14 holds waiting. In Owen’s real script, the pause is a network request to the kiosk.
Here’s the JavaScript port, saved as 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 it with node branch-status.js, and after the same short pause, JavaScript prints the same line:
Northgate: 14 holds waiting
In the return line, { branch, holds: kiosk.holds } uses object shorthand, so the branch key takes the value of the branch variable.
The function itself is the part that carries over. You write an async function with async function where Python uses async def. As in Python, await waits for the result and hands it over, pausing only its own function, not the whole program. If you prefer arrows, const checkKiosk = async (branch) => { ... }; works the same way, with async in front of the parameter list.
Python’s rule for await carries over: it works inside an async function. JavaScript also allows it at a module’s top level, which a plain Python file doesn’t. When you port a helper, an await in a plain function, such as an arrow function passed to .forEach() inside an async function, breaks that rule as in Python, but here it stops the file with SyntaxError: Unexpected reserved word.
Drop asyncio.run() and Await at the Top Level
Look at the bottom of each file. Python needs asyncio.run(), usually wrapped around a main() coroutine, because nothing runs coroutines until you start an event loop and hand it one. The JavaScript port needs neither asyncio.run() nor a main() wrapper, and that’s the real difference in this lesson.
JavaScript’s event loop is already running when your file starts. That’s why a module can await at its top level, as the port does with const kiosk = await checkKiosk("Northgate"). It’s the same await you put before readFile() at the top of a module, and now you know why it works. While the module waits, the pending sleep() timer keeps Node running until it fires. How the loop schedules that work comes later.
A longer script often keeps its steps in a main() function anyway, as Owen’s does. Then the end of the port looks like this:
async function main() {
const kiosk = await checkKiosk("Northgate");
console.log(`${kiosk.branch}: ${kiosk.holds} holds waiting`);
}
if (import.meta.main) await main();
Here import.meta.main is true when you run the file directly. The last line is JavaScript’s if __name__ == "__main__":, with await in the place of asyncio.run().
Pause With sleep() in Milliseconds
The port’s first line replaces asyncio.sleep(). Node’s node:timers/promises module has a setTimeout() function you can await, and import { setTimeout as sleep } imports it as sleep, much like Python’s from ... import ... as .... The name sleep says what the function does and distinguishes it from the global setTimeout().
Watch out for that global one, which expects a callback as its first argument, so await setTimeout(120) doesn’t pause but throws a TypeError, because 120 isn’t a function.
The other change is the unit. Python’s timers count seconds, while JavaScript’s count milliseconds, which is why Northgate’s delay became 120 in the port. Try changing Northgate’s delay in KIOSKS to 0.12. The line appears almost at once because Node waits about a millisecond instead of 120.
A JavaScript developer writes this the way you did: an async function with await inside, no asyncio.run(), and sleep() given milliseconds.
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.