Explicit resource management in JavaScript. Matt Smith breaks down the new using and await using declarations, plus Symbol.dispose and Symbol.asyncDispose, so cleanup happens automatically at scope exit, in reverse order for stacked resources. He also notes DisposableStack and AsyncDisposableStack for conditional lifetimes. #js #ecmasсript
https://allthingssmitty.com/2026/02/02/explicit-resource-management-in-javascript/
@webstandards_dev Vysvětlíte mi někdo, proč
const file = await openFile("data.txt");
const lock = await acquireLock();
try {
// work
} finally {
await lock.release();
await file.close();
}
má být
You write:
await using file = await openFile("data.txt");
using lock = await acquireLock();
a ne
You write:
await using file = await openFile("data.txt");
await using lock = await acquireLock();
?
- replies
- 1
- announces
- 0
- likes
- 0
@segedacz @webstandards_dev > Synchronous resources (locks, in-memory structures) can use plain using. It may feel odd at first, but it matches how JavaScript already draws the line between sync and async elsewhere. What matters is that cleanup happens at scope exit.
@segedacz @webstandards_dev jinak to asi poznáš podle toho, jestli implementuje Symbol.asyncDispose nebo Symbol.dispose.