makeDisposable
function makeDisposable<T>(resource, __namedParameters?): Promise<MadeDisposable<T>>;Defined in: make-disposable.ts:43
Wrap a resource with an asyncDispose handler so it can be used with await using.
Assigns Symbol.asyncDispose directly on the object (no Proxy).
Protects against double-dispose and accidental overwrite.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends object |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
resource |
T |
__namedParameters? |
MakeDisposableOptions<T> |
Returns
Section titled “Returns”Promise<MadeDisposable<T>>
Throws
Section titled “Throws”If the object already has a dispose handler and overwrite is not true.
Example
Section titled “Example”import { makeDisposable, DisposedSymbol } from "@xtandard/lib";
await using client = await makeDisposable( redis.createClient({ password: REDIS_PASSWORD }), { onInit: async (resource) => await resource.connect(), onDispose: async (resource) => await resource.disconnect(), },);
client[DisposedSymbol]; // false