Skip to content

await using

In JavaScript, the await using keyword declares a block-scoped constant whose value is cleaned up asynchronously when the enclosing scope exits. On exit, JavaScript calls and awaits the value’s [Symbol.asyncDispose]() method, or [Symbol.dispose]() when the async one is missing. It’s the asynchronous form of using, and it’s allowed only where await is: in async functions, at the top level of a module, and in for loop heads inside them.

Disposal runs in reverse declaration order, even when the scope exits by throwing, much like a finally block. If a disposer throws while another error is pending, JavaScript wraps both in a SuppressedError. A null or undefined value skips disposal, and any other value without a disposer throws a TypeError. Like a const binding, it can’t be reassigned, but unlike const, it can’t be destructured.

JavaScript await using Keyword Syntax

An await using declaration can stand on its own or start a for...of loop:

Language: JavaScript Syntax
await using <name> = <value>[, <name2> = <value2>, ...];

for (await using <name> of <iterable>) {
  <statements>
}

JavaScript await using Keyword Examples

Two connections declared with await using close in reverse order when the block ends, before the next line runs:

Language: JavaScript Filename: connections.mjs
function openConnection(name) {
  console.log(`open ${name}`);
  return {
    async [Symbol.asyncDispose]() {
      await new Promise((resolve) => setTimeout(resolve, 10));
      console.log(`close ${name}`);
    },
  };
}

{
  await using db = openConnection("db");
  await using cache = openConnection("cache");
  console.log("working");
}
console.log("after block");

Running the file shows when each resource closes:

Language: Shell
$ node connections.mjs
open db
open cache
working
close cache
close db
after block

The block closes cache before db, and the code after it runs only once both disposers have finished.

A synchronous [Symbol.dispose]() method also works, and it runs before the error leaves the function:

Language: JavaScript
> const lock = {
…   [Symbol.dispose]() {
…     console.log("released");
…   },
… };
undefined
> async function update() {
…   await using guard = lock;
…   throw new Error("update failed");
… }
undefined

> await update().catch((error) => error.message)
released
'update failed'

JavaScript await using Keyword Use Cases

  • Closing file handles, streams, and database connections that need async cleanup
  • Releasing locks and other resources even when an error is thrown
  • Replacing try/finally blocks that await a cleanup call
  • Disposing of each value of an iteration with for (await using ... of ...)

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026