AsyncDisposable Namespace
Package: @esfx/disposable
Functions
create(disposeAsync)
Creates an AsyncDisposable
wrapper around a callback used to dispose resources.
NOTE: This is not spec-compliant and will not be standardized. It is preferred to use an AsyncDisposableStack
or to implement AsyncDisposable.asyncDispose
yourself instead.
Declaration
function create(disposeAsync: () => void | PromiseLike<void>): AsyncDisposable;
Parameters
- disposeAsync
- () => void | PromiseLike<void>
Returns
hasInstance(value)
Determines whether a value is AsyncDisposable
.
NOTE: This is not spec-compliant and will not be standardized.
Declaration
function hasInstance(value: unknown): value is AsyncDisposable;
Parameters
- value
- unknown
Returns
scope()
Emulate using await
using for..await..of
.
NOTE: This is not spec-compliant and will not be standardized.
Declaration
function scope(): AsyncGenerator<AsyncDisposableScope, void, undefined>;
Returns
Examples
// with `using await` (proposed)
{
...
using await x = expr, y = expr;
...
}
// with `AsyncDisposable.scope()`:
for await (const { using, fail } of AsyncDisposable.scope()) {
try {
...
const x = using(expr), y = using(expr);
...
}
catch (e) {
fail(e);
}
}
usingEach(iterable)
Yields each disposable in the iterable, disposing it when the generator resumes.
This emulates for (using await x of expr)
.
NOTE: This is not spec-compliant and will not be standardized.
Declaration
function usingEach(iterable: AsyncIterable<AsyncDisposable | Disposable | null | undefined> | Iterable<AsyncDisposable | Disposable | null | undefined | PromiseLike<AsyncDisposable | Disposable | null | undefined>>): AsyncGenerator<AsyncDisposable | Disposable | null | undefined, void, unknown>;
Parameters
- iterable
- AsyncIterable<AsyncDisposable | Disposable | null | undefined> | Iterable<AsyncDisposable | Disposable | null | undefined | PromiseLike<AsyncDisposable | Disposable | null | undefined>>
Returns
Examples
// with `using await` (proposed)
for (using await x of expr) {
...
}
for await (using await x of expr) {
...
}
// with `Disposable.usingEach()`:
for await (const x of Disposable.usingEach(expr)) {
...
}
Variables
asyncDispose
A well-known symbol used to define an async explicit resource disposal method on an object.
NOTE: Uses Symbol.asyncDispose
if present.
Declaration
asyncDispose: AsyncDisposeSymbol