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

Collecting Cleanup With AsyncDisposableStack

Collecting Cleanup With AsyncDisposableStack

Priya’s final main() connects a press’s queue only when the first job for that press comes up, so the runner can’t declare its queues in advance. Python collects them on an AsyncExitStack: callback(remove_spool, folder) registers the folder’s removal with its argument, and enter_async_context() connects each queue and adds it. When the async with block ends, everything is released last first.

JavaScript’s counterpart is the built-in AsyncDisposableStack. The stack is an async disposable itself, so you declare it with await using in a block of its own, and the summary prints after the block, as Python’s prints after the async with.

mkdtemp() from node:fs/promises makes the spool folder, like tempfile.mkdtemp(), but only Python’s puts it in the temporary folder for you, so the prefix is joined to tmpdir() (from node:os, like tempfile.gettempdir()) with join() (from node:path, like os.path.join()). removeSpool() deletes it with rm(folder, { recursive: true }), like shutil.rmtree().

stack.use() is enter_async_context(), except that it doesn’t connect anything: it takes a queue that’s already open, adds it to the stack and returns it, so it can go straight into the Map. stack.defer() is callback(), except that it takes one function and calls it with no arguments. Ported line for line, stack.defer(removeSpool, folder) runs the whole morning, closes both queues and then stops.

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 In this lesson, you’ll collect the runner’s cleanup on an AsyncDisposableStack, and watch a morning crash on its very last step. Priya’s final main() connects each press’s queue only when its first job comes up.

00:14 So the runner can’t know how many queues to close, and two fixed await using lines won’t do.

00:22 In Python, Priya’s AsyncExitStack holds the folder’s callback, and the queue for each press as it comes up. When the async with block ends, it releases them last first.

00:37 AsyncDisposableStack is JavaScript’s built-in counterpart. The stack is itself an async disposable, so you declare it with await using, in a block of its own.

00:48 When that block ends, the stack disposes everything it holds, and the summary prints after it, as in Python.

00:56 The spool folder comes from mkdtemp() in node:fs/promises, like tempfile.mkdtemp(). Only Python’s puts it in the temporary folder for you, so the JavaScript joins tmpdir() and the prefix itself.

01:11 Priya’s remove_spool() becomes your port’s removeSpool(), where rm() with recursive does what shutil.rmtree() does.

01:20 For callback(), the stack has defer(): it registers a function to run when the stack is disposed. So Priya’s call becomes stack.defer(removeSpool, folder), the folder after the function.

01:36 The queues go in a Map, keyed by press, like Priya’s dict. If the Map doesn’t have the job’s press yet, the runner connects its queue first, with an await of its own.

01:47 Then stack.use() adds the queue to the stack and returns it, so it goes straight into the Map. Unlike enter_async_context(), use() doesn’t connect anything: it takes a queue that’s already open.

02:02 Run the morning. The jobs print as before, ending with J-314’s skipped line. Then the block ends, and the stack releases its queues, last first: press-2 closes, then press-1.

02:18 The folder’s removal went on first, so it runs last. Your turn: pause the video and predict what it prints.

02:30 No spool line at all. The run stops with a TypeError: rm() received undefined as its path.

02:41 The trace points to removeSpool(), at the line that calls rm(). The error ends the program, so the summary never prints, and the exit code is 1.

02:52 And the spool folder is still there, in the system’s temporary folder.

02:58 Python’s callback() keeps the arguments after the function and passes them on. defer() takes only the function. JavaScript ignores the extra one, and the stack calls removeSpool() with no arguments.

03:12 In Python, that call would raise a TypeError straight away. JavaScript doesn’t mind a missing argument: the parameter is undefined, and the function runs until rm() checks it.

03:25 The fix is a closure: an arrow function, like a Python lambda, that calls removeSpool() with the folder. The stack calls the arrow with nothing, and the arrow passes folder on.

03:37 Being async, this stack awaits the arrow’s promise, so here defer() is callback() and push_async_callback() in one.

03:46 Run the morning again. After both queues close, the folder is removed, and the last three lines match Python’s.

03:55 For synchronous resources, DisposableStack with plain using does what Python’s ExitStack does. And stack.move() is pop_all(): it hands everything to a new stack, which releases it later, and leaves the old one disposed.

04:13 When a block and its cleanup both throw, the catch gets a SuppressedError holding both. Its error is the cleanup’s, and its suppressed is the block’s.

04:23 So a check on err.code, like the runner’s, finds nothing: test for a SuppressedError and look inside. In Python, too, you don’t catch the block’s error: the cleanup’s exception comes out, with the block’s chained on.

04:40 That’s the JavaScript way: one stack declared with await using, use() for each resource as you open it, and defer() with an arrow function for any other cleanup.

04:50 Next up, Run the Finishing Machines, an exercise: after printing, jobs go through the finishing room, where each machine is switched on the first time a job needs it and off again at the end.

Become a Member to join the conversation.