demo · v134

await using

await using db = await openDb() defers db[Symbol.asyncDispose]() until the block exits. The buttons run real await using blocks against an instrumented async disposable resource and stream the lifecycle to the log — including the throw-and-dispose ordering.

probing Symbol.asyncDispose...

Try it

The code

async function openDb(name) {
  return {
    name,
    async [Symbol.asyncDispose]() { console.log("closing", name); }
  };
}

async function example() {
  await using db = await openDb("primary");
  // ... use db ...
}                                            // db is async-disposed HERE
// (even if an exception is thrown)

see also