Resource
Resource:
object
Defined in: Core/Resource.ts:34
Type Declaration
Section titled “Type Declaration”all: <
R>(resources) =>Resource<InferError<R>,InferValue<R>>
Sequentially acquires an array/tuple or named record of resources.
All resources are released in strict reverse (LIFO) order upon completion.
If the K-th resource fails to acquire, all already-acquired resources (K-1 … 0)
are released in reverse order before Resource.use returns the error.
Type Parameters
Section titled “Type Parameters”R extends Readonly<Record<string, Resource<any, any>>> | readonly Resource<any, any>[]
Parameters
Section titled “Parameters”resources
Section titled “resources”R
Returns
Section titled “Returns”Resource<InferError<R>, InferValue<R>>
Resource.combine for binary resource pairing.
Example
Section titled “Example”chain: <
A,E2,B>(transform) => <E1>(resource) =>Resource<E2|E1,B>
Sequentially acquires a second resource that depends on the value of the first.
If acquiring the second resource returns an error, Resource.use releases the first resource before returning
the error. When Resource.use completes, both resources are released in reverse (LIFO) order: the second is
released before the first.
Type Parameters
Section titled “Type Parameters”A
E2
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => Resource<E2, B>
Returns
Section titled “Returns”<E1>(resource) => Resource<E2 | E1, B>
Resource.map for transforming acquired values without opening a new resource.
Example
Section titled “Example”combine
Section titled “combine”combine: <
E1,A,E2,B>(resourceA,resourceB) =>Resource<E1|E2, readonly [A,B]>
Acquires two resources in sequence and presents them as a tuple.
Resources are released in reverse order: the second is released before the first.
If the second resource fails to acquire, the first is released before
Resource.use returns the error.
Type Parameters
Section titled “Type Parameters”E1
A
E2
B
Parameters
Section titled “Parameters”resourceA
Section titled “resourceA”Resource<E1, A>
resourceB
Section titled “resourceB”Resource<E2, B>
Returns
Section titled “Returns”Resource<E1 | E2, readonly [A, B]>
Resource.all for acquiring arbitrary tuples or named records of resources.
Example
Section titled “Example”make: <
E,A>(acquire,release) =>Resource<E,A> =makeResource
Creates a Resource from an acquire operation that may fail and a release finalizer.
Convert an infallible acquisition with Task.Result.from.Task before passing it here.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”acquire
Section titled “acquire”Result<E, A>
release
Section titled “release”(value) => Task<unknown>
Returns
Section titled “Returns”Resource<E, A>
Example
Section titled “Example”map: <
A,B>(transform) => <E>(resource) =>Resource<E,B>
Transforms the value of an acquired resource while retaining guaranteed cleanup of the original asset.
The transform must not throw.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => B
Returns
Section titled “Returns”<E>(resource) => Resource<E, B>
Resource.chain for sequencing dependent resource acquisitions.
Example
Section titled “Example”use: <
A,E2,B>(options) => <E1>(resource) =>Deferred<Result<E2|E1,B>>
Acquires the resource, runs the supplied work, then releases it.
Successfully acquired resources are released in reverse order when acquisition or work returns an error. If acquisition returns an error, work and its release are skipped. Release tasks and callbacks must not throw or reject.
The run callback parameter must be annotated: the resource arrives after the options,
so TypeScript cannot infer its value type from the resource.
Type Parameters
Section titled “Type Parameters”A
E2
B
Parameters
Section titled “Parameters”options
Section titled “options”(value) => Result<E2, B>
signal?
Section titled “signal?”AbortSignal
Returns
Section titled “Returns”<E1>(resource) => Deferred<Result<E2 | E1, B>>