Mrs. Sari closes the warung with await using: the stove, lamp and radio switch off by themselves, but in what order? What if the food burns, or the stove jams…
Why it matters
Database connections, files, locks, event subscriptions: all must be closed, errors included. try/finally makes it easy to forget one, and the closing order is often wrong. await using (and AsyncDisposableStack) makes async cleanup run by itself, in order, and awaited.
The key idea
Disposed of in reverse, on every way out, and awaited
Common mistakes
Using plain using, thinking its cleanup is awaited while it is only synchronoususing calls [Symbol.dispose]() and does not wait for any promise it returns. For async cleanup, use await using with [Symbol.asyncDispose]().
Thinking disposers run in declaration order, while the last one opened closes first The order is reversed (LIFO), like a stack: whatever depends on an earlier resource closes first. Declare the most basic one (the connection) first.
await using in a scope that is too wide: the resource is held long after it is needed A resource is only disposed of at the end of its scope. Wrap its use in a block { } of its own so it is let go as soon as it is done with.
Try it in this playground
The last one on is the first one off Step until the radio goes off while the stove is still on.
Burnt, but still switched off Open Burnt halfway: a throw does not stop the cleanup.
A SuppressedError carries both errors Open The stove sticks and run it to the catch.
Plain using does not wait Watch something go off after 'warung tutup' prints.