defer
Runs an async function and guarantees that all deferred callbacks are executed afterwards, in LIFO order (last registered = first executed), regardless of whether the main work succeeds or throws.
Inspired by Radashi’s defer. Useful for resource cleanup, temporary file
removal, or any “undo” logic that must run even on failure.
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fn | function | An async function that receives a defer registration function. |
Returns
Section titled “Returns”Promise<T> — The resolved value of fn.
Examples
Section titled “Examples”Cleanup always runs
Section titled “Cleanup always runs”Registered callbacks execute after the main function, even on success.
LIFO order
Section titled “LIFO order”Multiple callbacks are called in reverse registration order.
Cleanup runs even on failure
Section titled “Cleanup runs even on failure”Callbacks still execute when the main function throws; the error is re-thrown after.
