defer
function defer(fn, options?): Deferred;Defined in: defer.ts:81
Creates an async disposable with cancellation, resume, and double-dispose protection.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
fn |
() => MaybePromise<void> |
options? |
DeferOptions |
Returns
Section titled “Returns”Examples
Section titled “Examples”Basic usage
await using cleanup = defer(async () => { await closeConnection();});With AbortSignal
const controller = new AbortController();await using cleanup = defer(async () => { await closeConnection();}, { signal: controller.signal });
controller.abort(); // cleanup is now canceledWith onDispose callback
await using cleanup = defer(async () => { await closeConnection();}, { onDispose: () => console.log("disposed") });Cancel with reason
const d = defer(() => { ... });d.cancel("no longer needed");d.cancelReason; // "no longer needed"