await using & Promise.withResolvers (ES2024) in JavaScript

From the Async JavaScript cheat sheet · Async/Await · verified Jul 2026

await using & Promise.withResolvers (ES2024)

Explicit resource management with `using` + the deferred-promise helper

javascript
// await using — auto-dispose async resources when the block ends
class DbConnection {
  async [Symbol.asyncDispose]() { await this.close() }
}

async function getUser(id) {
  await using conn = await pool.acquire()   // released no matter how we exit
  return conn.query('SELECT * FROM users WHERE id = $1', [id])
}

// Promise.withResolvers — get a promise + its resolve/reject up front
function once(emitter, event) {
  const { promise, resolve, reject } = Promise.withResolvers()
  emitter.once(event, resolve)
  emitter.once('error', reject)
  return promise
}
💡 `await using x = ...` is the cleanup-guaranteed alternative to try/finally
📌 Implement Symbol.dispose / Symbol.asyncDispose to make any class disposable
⚡ Multiple disposables release in REVERSE order — predictable teardown
🎯 Promise.withResolvers replaces the hand-rolled "deferred" pattern
es2024resource-managementmodern

More JavaScript tasks

Back to the full Async JavaScript cheat sheet