export type Destructor = () => void | PromiseLike; export interface Destructable { destroy: Destructor; } export type Usable = TDestructable | PromiseLike; export async function use( usable: Usable, job: (destructable: TDestructable) => TResult, ): Promise { const destructable = await usable; try { const result = await job(destructable); return result; } finally { await destructable.destroy(); } }