Resource
Resource:
object
Defined in: Core/Resource.ts:35
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 or throws, all already-acquired resources (K-1 … 0) are immediately released in reverse order before propagating 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 fails or throws, the first resource is released immediately before propagating
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 or throws, the first is released immediately before propagating 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”from:
object
from.Task
Section titled “from.Task”Task: <
A,E>(acquire,release) =>Resource<E,A> =makeTask
Creates a Resource from an acquire operation that cannot fail. Use this when opening the resource is guaranteed to succeed, such as in-memory locks, counters, or timers.
Type Parameters
Section titled “Type Parameters”A
E = never
Parameters
Section titled “Parameters”acquire
Section titled “acquire”Task<A>
release
Section titled “release”(value) => Task<unknown>
Returns
Section titled “Returns”Resource<E, A>
Resource.make for creating resources with fallible acquisition.
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.
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>
Resource.from.Task for creating resources with infallible acquisition.
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.
If transform throws an exception, the acquired resource is released immediately before propagating the error.
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>(consume) => <E1>(resource) =>Result<E2|E1,B>
Acquires the resource, runs consume with it, then releases it.
Release always runs, even when consume returns an error or throws.
If acquire fails, consume and release are both skipped and the error is returned.
Type Parameters
Section titled “Type Parameters”A
E2
B
Parameters
Section titled “Parameters”consume
Section titled “consume”(value) => Result<E2, B>
Returns
Section titled “Returns”<E1>(resource) => Result<E2 | E1, B>