Actualmente estoy haciendo algo como esto:
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 })¿Hay una mejor manera de lograr este comportamiento?