Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Holds Check

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:

Language: Program Output
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() from node:timers/promises, for asyncio.sleep()’s job, and keeps every delay, LIMIT and RETRY_PAUSE in milliseconds.
  • The stand-in throws a plain Error: JavaScript has no ConnectionError.
  • JavaScript has no wait_for(), so the timeout() helper sleeps for the limit, then throws.
  • checkWithRetry() keeps the first await inside the try, so the catch sees the failure. The catch takes every error, since JavaScript’s catch has no class filter and the stand-in fails one way. The second return, outside the try, drops its await, 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, where gather() takes separate arguments, and records every outcome, as return_exceptions=True does.
  • The loop walks the records with .entries() and tests each one’s status: a record is a plain object, never an error. A fulfilled record’s holds are in value.
  • if (import.meta.main) await main(); does the job of if __name__ == "__main__": and asyncio.run(main()), since JavaScript’s event loop is already running.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The library’s new staff dashboard is written in JavaScript, so Owen’s morning check moves too. His script asks the four kiosks at once, gives up on a slow one and asks a failing one again.

00:13 It also reads and writes a small history file. Run it on a first morning, and it prints seven lines.

00:22 You’ll port it up to the board and the total.

00:27 The port starts with sleep, from Node’s timers promises module: it does asyncio.sleep()’s job. The kiosks keep their four branches: names with a space keep their quotes, fails_first goes camel case, and every delay is in milliseconds.

00:45 BRANCHES lists their names. LIMIT and RETRY_PAUSE become five hundred and two hundred. The other imports are the exercise’s.

00:57 The stand-in ports line for line. It looks up the kiosk and awaits sleep() for its delay. A kiosk marked failsFirst clears the mark and throws a plain error, since JavaScript has no ConnectionError class.

01:12 Otherwise, it returns the branch and its holds.

01:17 Python’s wait_for() takes the limit as an argument. JavaScript has none, so the port brings the timeout() helper. It sleeps for the limit, then throws, so its promise can only reject.

01:32 The retry keeps Python’s shape: try, then catch, then a pause and one more try. The first await stays inside the try, so the catch sees the failure.

01:45 The catch has no class filter: it takes every error, and the stand-in fails only one way. The second return, outside the try, drops its await: the caller waits for it.

02:00 In main(), each kiosk’s check is a race: the retry against the timer, where Python wraps it in wait_for(). .map() builds one race per branch, and they all start at once.

02:14 allSettled() takes the array itself. Spreading it, as gather()’s star does, stops JavaScript with a TypeError, as you saw.

02:23 It records every outcome, as return_exceptions does.

02:29 The loop walks the records with .entries(), so each gets its index, and the index finds its branch, as zip() pairs them in Python.

02:37 A record is a plain object, never an error, so the test reads its status, where Python checks the result’s type. A rejected race prints no answer.

02:48 A fulfilled one reads its holds from value and adds them to the total. Then the total line follows.

02:58 The rest of Python’s main() waits for yesterday’s total, writes today’s and times the check: that part is the exercise’s. At the bottom, import.meta.main does the job of the __name__ check.

03:12 And await main() replaces asyncio.run(), since JavaScript’s event loop is already running.

03:20 node --check reads the file and prints nothing: the syntax is fine. Now run it. Northgate and Eastfield come first, and Hollins Green has no answer: the timer won its race.

03:35 Quarry Hill’s first answer fails. The retry pauses and asks again, inside the race. Is its record fulfilled or rejected? Your turn: pause the video and predict.

03:53 Fulfilled.

03:57 The retry caught the failure, so allSettled() sees only the second answer: Quarry Hill has three holds. The total is twenty-six. Run the Python, and its first five lines match the port’s exactly.

04:15 The board is done at half a second, but JavaScript ends at nine tenths: the race only stopped waiting, and Hollins Green’s check runs on until it answers.

04:26 That’s the check, the JavaScript way: allSettled() for return_exceptions, and a race against a timer for wait_for().

04:35 try and catch around await do the retry, and await main() stands in for asyncio.run()’s job.

04:43 Next up, the exercise Finish the Holds Check. You’ll load yesterday’s total while the kiosks are asked, write today’s to the history file and time the check, until the port prints all seven lines.

Become a Member to join the conversation.