Skip to content

Task

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

Defined in: Core/Task.ts:42

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.

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

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();