Skip to content

Instantly share code, notes, and snippets.

@rostegg
Last active January 29, 2022 22:03
Show Gist options
  • Save rostegg/8a468ff76d61453c067d6e04c5f1a17c to your computer and use it in GitHub Desktop.
Save rostegg/8a468ff76d61453c067d6e04c5f1a17c to your computer and use it in GitHub Desktop.
Async disposer pattern example (simple way to controll freeing a resource)
const withResource = async (fn) => {
const instance = await getInstance()
try {
return await fn(instance)
} finally {
await instance.close()
}
}
const task = await witchResource(async (instance) => {
const data = await instance.getData()
return data
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment