Loading video player…

Welcome to Ashdown Libraries' Holds Check

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():

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 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:

Language: Program Output
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:

Language: JavaScript
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:

Language: Program Output
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 def and await without asyncio.run(), and read the promise an async call returns
  • Await kiosks in turn with for...of, all at once with Promise.all(), and keep every outcome with Promise.allSettled()
  • Give up on slow work with Promise.race(), retry with try/catch around await, 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.

Download

Sample Code (.zip)

14.4 KB
Download

Course Slides (.pdf)

970.3 KB

00:00 Welcome to Async JavaScript for Pythonistas! You’ll port asyncio code to JavaScript: async functions, promises, many checks at once, timeouts, retries, and failures nobody’s waiting for.

00:14 Your project: 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 and prints twenty-nine holds waiting.

00:33 Port it line by line, and Python’s star becomes three dots, JavaScript’s spread. Run it, and JavaScript stops before a single kiosk answers. The TypeError says an object isn’t iterable, as Python’s own would.

00:50 It stops at this call, 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.

01:02 Where it breaks is where JavaScript and asyncio differ, and the course takes each break in turn.

01:10 Ashdown Libraries’ four branches each have a holds kiosk. Every morning, Owen’s script asks them all and prints a board. The staff dashboard is moving to JavaScript.

01:24 By the end, you’ll port async functions, read their promises, wait for many kiosks at once, handle slow and failing ones, and port Owen’s whole script.

01:36 You only need to know Python’s asyncio, and JavaScript’s functions, modules and errors.

01:44 Videos mix with text lessons and exercises, and every video has a summary. Next up: Porting async def Without asyncio.run().

Become a Member to join the conversation.