Skip to content

Task

Task<A> = (signal?) => Deferred<A>

Defined in: Core/Task.ts:46

A lazy async computation that always resolves.

Two guarantees:

  • Lazy — nothing starts until you call it.
  • Infallible — it never rejects. If failure is possible, encode it in the return type using TaskResult<E, A> instead.

An optional AbortSignal can be passed at the call site. Combinators like retry, pollUntil, and timeout thread it automatically to every inner operation. Existing tasks that ignore the signal continue to work unchanged.

Calling a Task returns a Deferred<A> — a one-shot async value that supports await but has no .catch(), .finally(), or chainable .then().

Consuming a Task:

Use await task() to run it and get the value directly:

const value: number = await task();

When you need an explicit Promise<A> (e.g. for a third-party API), convert the Deferred with Deferred.toPromise:

const p: Promise<number> = Deferred.toPromise(task());

A

AbortSignal

Deferred<A>

const getTimestamp: Task<number> = Task.resolve(Date.now());

// Nothing runs yet — getTimestamp is just a description
const formatted = pipe(
  getTimestamp,
  Task.map(ts => new Date(ts).toISOString())
);

// Execute when ready
const result = await formatted();