Currently I am doing sth like this:
declare type Resource = { dispose: () => void }
declare function getResource(): Promise<Resource>
async function withResource<T>(executor: (r: Resource) => T) {
const r = await getResource()
try {
return await executor(r)
} finally {
r.dispose()
}
}
...
withResource(async r => {
// code goes here
})
Is there a better way to achieve this behavior ?