From Python's with to JavaScript's using and try/finally (Summary)

Priya’s job runner now runs in JavaScript next to Inkwell & Co.’s order-tracking app. It prints the same 27 lines as runner.py, cleans up every lock, spool copy, queue, and the spool folder even when J-313 fails, and leaves the other PC’s J-314.lock where it found it.

Each Python Tool and Its JavaScript Port

In this course, you learned how to port each of these:

Python JavaScript
with the acquiring line, then try/finally
the setup in __enter__(), and __exit__() the constructor and [Symbol.dispose](), held with using in a block as long as the with (no enter step: the variable is the object itself)
@contextmanager a function that returns a disposable
async with and __aexit__() await using and [Symbol.asyncDispose]()
async setup in __aenter__() a static async factory, PrinterQueue.connect()
AsyncExitStack AsyncDisposableStack with stack.use() and stack.defer()

The first two rows show different ports of the same with. Either you write the cleanup yourself in a try/finally, or the object has a dispose method and you hold it with using or await using. A stack collects resources you acquire as you go, like the runner’s queues.

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

Become a Member to join the conversation.