Closing a Connection With await using

Your port’s lock and spool copy now release themselves with using, but main() still closes the print queues in a finally. Priya’s Python needs no finally for the queues, because PrinterQueue is an async context manager. A queue takes a moment to close, so [Symbol.dispose]() won’t do: using calls it and moves on without awaiting the close. In this lesson, you’ll give the queue a cleanup method that JavaScript waits for.

Give the Queue an Async Dispose Method

Python’s async with awaits __aenter__() on the way in and __aexit__() on the way out. JavaScript’s protocol has only the release half, so the two methods land in different places:

Python JavaScript
async def __aenter__(self) static async connect(press)
async def __aexit__(self, *exc_info) async [Symbol.asyncDispose]()

The release half uses a second built-in symbol, Symbol.asyncDispose. An object with a [Symbol.asyncDispose]() method that returns a promise is an async disposable, the counterpart of an object with __aexit__().

Copy your port to runner-s3.js. In the class, close() becomes async [Symbol.asyncDispose](), with the same body:

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.