Skip to content

Deferred

Deferred: object

Defined in: Core/Deferred.ts:24

all: <T>(deferreds) => Deferred<{ [K in string | number | symbol]: T[K] extends Deferred<A> ? A : never }>

Combines an array or tuple of Deferred values into a single Deferred of a tuple. Resolves when all input Deferreds resolve.

T extends readonly Deferred<unknown>[]

T

Deferred<{ [K in string | number | symbol]: T[K] extends Deferred<A> ? A : never }>

const [a, b] = await Deferred.all([d1, d2]);

chain: <A, B>(transform) => (deferred) => Deferred<B>

Chains a Deferred-returning step after the current one resolves.

The next step starts only after the previous value arrives, and only that value is passed forward. Use Deferred.all to run independent steps concurrently.

A

B

(value) => Deferred<B>

(deferred) => Deferred<B>

const orders = pipe(
  Task.run()(loadUser("u_1")),
  Deferred.chain((user) => Task.run()(loadOrders(user.id))),
);

from: object

Promise: <A>(p) => Deferred<A> = fromPromise

Wraps a Promise or Deferred into a Deferred, structurally excluding rejection handlers, .catch(), .finally(), and chainable .then().

Precondition: p must never reject. If p rejects, the returned Deferred will never resolve — await-ing it will hang indefinitely. Use Task.Result.tryCatch to handle operations that may fail before converting to a Deferred.

A

Thenable<A>

Deferred<A>

const d = Deferred.from.Promise(Promise.resolve("hello"));
const value = await d; // "hello"

map: <A, B>(transform) => (deferred) => Deferred<B>

Transforms the resolved value of a Deferred.

Deferred is the type every asynchronous run settles into: Task.run, Op.Manager.run, Resource.use, and Graph.run all return one. Use map to reshape that settled value without converting back to a Promise.

A

B

(value) => B

(deferred) => Deferred<B>

Deferred.chain when the transform itself returns another Deferred.

const total = pipe(
  Task.run()(loadCart()),
  Deferred.map((cart) => cart.items.length),
);
console.log(await total); // 3

race: <A>(deferreds) => Deferred<A>

Races multiple Deferred values and resolves with the first one to settle.

A

readonly Deferred<A>[]

Deferred<A>

const winner = await Deferred.race([d1, d2]);

tap: <A>(sideEffect) => (deferred) => Deferred<A>

Runs a side effect with the resolved value and passes that value through unchanged.

The callback must not throw. Use it to log, record metrics, or update a store after an operation settles without breaking the chain.

A

(value) => void

(deferred) => Deferred<A>

Deferred.map to transform the value instead of observing it.

const outcome = pipe(
  Task.run()(chargeCard(payment)),
  Deferred.tap((receipt) => audit.record(receipt.id)),
);

to: object

Promise: <A>(d) => Promise<A> = toPromise

Converts a Deferred back into a Promise.

A

Deferred<A>

Promise<A>

const p = Deferred.to.Promise(Deferred.from.Promise(Promise.resolve(42)));
// p is Promise<42>