demo · v131

using - synchronous resource disposal

using binds a resource and guarantees its [Symbol.dispose] runs at scope exit, even on a thrown error. Compare the manual try/finally with the new statement.

without using (try/finally)

with using (auto dispose)

the api

function acquireLock() {
  return { release: () => {}, [Symbol.dispose]: () => release() };
}

{
  using lock = acquireLock();  // disposed when scope ends
  doWork();
}                              // [Symbol.dispose] fires here

see also