Aller au contenu principal

Overview

Lazy, composable and cancelable async computations

API Github versionNPM LicenseBundle size

Installation

yarn add @w5s/task

Motivation

A Task<Value, Error> is a lazy computation that will be evaluated later. Unlike Promises, Tasks:

  • Are lazy: Nothing happens until you explicitly run them
  • Are composable: Chain operations without triggering execution
  • Are cancelable: Stop running tasks when no longer needed
  • Return Results: Always resolve to Result<Value, Error>, making error handling explicit

This makes Tasks ideal for:

  • Side effects management
  • Complex async workflows
  • Testable async code
  • Resource management with cancellation

Quick Start

import { Task } from '@w5s/task';

// Create a task that generates a random number
function randomNumber() {
return Task.create(() => Task.ok(Math.random()));
}

// Create a task that logs a value
function log(value: unknown) {
return Task.create(() => Task.ok(console.log(value)));
}

// Compose tasks
export function main() {
const randomValueTask = randomNumber();
const squareValueTask = Task.map(randomValueTask, (value) => value * value);
return Task.andThen(squareValueTask, log);
}

// Run at the edge of your program
void Task.run(main());