Priya’s job runner now uses JavaScript next to Inkwell & Co.’s order-tracking app. Each morning it acquires every job’s lock, spools the artwork, sends it to press-1 or press-2, and releases each job’s resources, even when a job fails halfway. And it still leaves the other PC’s J-314.lock exactly where it found it.
In Python, with and its context managers made that cleanup automatic. JavaScript’s tools for it are newer, and they differ in details that matter when porting code: where the acquiring line goes, how long a resource is held, and what gets awaited. In this course, you learned how to:
- Release a resource with
try/finally, acquiring it on the line before thetry. - Recognize calls that manage files they open from paths, such as
readFile(),writeFile(), andcopyFileSync(). Identify resources you must close: caller-supplied handles or descriptors, aFileHandlefromopen(), a timer fromsetInterval(), and a connection like the print queue’s. - Give a class a
[Symbol.dispose]()method and hold an instance withusingin a block that lasts as long as thewithblock did. - Replace a function decorated with
@contextmanagerwith a function that returns a disposable object. - Close a connection with
await usingand[Symbol.asyncDispose](), with its asynchronous setup in a static async factory such asPrinterQueue.connect(). - Collect a changing set of resources with an
AsyncDisposableStack, usingstack.use()for each queue andstack.defer()for the spool folder.
Those pieces add up to the runner’s main(), which opens only the queues the morning needs and closes them in reverse order when its block ends.