Porting the Holds Check
The library’s new staff dashboard is written in JavaScript, so Owen’s morning check moves too. His Python script asks the four kiosks at once, gives up on a slow one and asks a failing one again. It also reads and writes a small history file. On a first morning, it prints seven lines:
Northgate: 14 holds waiting
Eastfield: 9 holds waiting
Hollins Green: no answer
Quarry Hill: 3 holds waiting
Total: 26 holds waiting
Yesterday: no history yet
Checked 4 kiosks in 0.5 s
In this lesson, you ported it to JavaScript up to the board and the total, function by function:
- The port starts with
sleep()fromnode:timers/promises, forasyncio.sleep()’s job, and keeps every delay,LIMITandRETRY_PAUSEin milliseconds. - The stand-in throws a plain
Error: JavaScript has noConnectionError. - JavaScript has no
wait_for(), so thetimeout()helper sleeps for the limit, then throws. checkWithRetry()keeps the firstawaitinside thetry, so thecatchsees the failure. Thecatchtakes every error, since JavaScript’scatchhas no class filter and the stand-in fails one way. The secondreturn, outside thetry, drops itsawait, since the caller waits for that promise, where Python’s caller would get a coroutine.- In
main(),.map()starts one race per branch, the retry against the timer.Promise.allSettled()takes that array itself, wheregather()takes separate arguments, and records every outcome, asreturn_exceptions=Truedoes. - The loop walks the records with
.entries()and tests each one’sstatus: a record is a plain object, never an error. A fulfilled record’s holds are invalue. if (import.meta.main) await main();does the job ofif __name__ == "__main__":andasyncio.run(main()), since JavaScript’s event loop is already running.