# t-tasks

> ![CI](https://github.com/lammonaaf/t-ask/workflows/CI/badge.svg?branch=master)

Latest version **2.1.1** (published 2026-10-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install t-tasks
pnpm add t-tasks
yarn add t-tasks
bun add t-tasks
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2026-10-12 |
| First published | 2020-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 416.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Alexander Yushkov |
| Maintainers | lammonaaf |
| Keywords | tasks, flow, monad, cancelable |

## Links

- npm: https://www.npmjs.com/package/t-tasks
- Repository: https://github.com/lammonaaf/t-tasks
- Homepage: https://lammonaaf.github.io/t-tasks
- Issues: https://github.com/lammonaaf/t-tasks/issues
- npm.io page: https://npm.io/package/t-tasks

## Recent versions

- 2.1.1 (latest) — 2026-10-12
- 2.1.0 — 2026-10-12
- 2.0.0 — 2026-09-28
- 1.2.1 — 2026-04-19
- 1.2.0 — 2026-04-18
- 1.1.2 — 2025-11-27
- 1.1.1 — 2024-02-06
- 1.0.4 — 2022-10-04
- 1.0.3 — 2022-10-04
- 1.0.2 — 2022-09-27
- 1.0.1 — 2022-07-21
- 1.0.0 — 2022-01-17
- 1.0.0-beta.1 — 2022-01-17
- 0.9.6 — 2021-11-07
- 0.9.5 — 2021-06-17
- … 14 more at https://npm.io/package/t-tasks/versions

## README

![CI](https://github.com/lammonaaf/t-ask/workflows/CI/badge.svg?branch=master)

# T-Tasks task library

T-Tasks provides a Task quasi-monad, allowing to schedule, cancel and externally reject async operations in a composable, semi-functional way.

Sometimes (for example, within React hooks) you need to cancel any ongoing asynchronous operation to prevent side-effects. Switching from async functions to chained tasks one can simply cancel current task execution ensuring that the operation will be cancelled at the nearest spot, preventing any unwanted side efects afterwards.

## Examples

### Prevent side-effects after async function

Keep in mind, that pure promises are still not cancelable, so the original function will still finish. In this scenario we can only elliminate side effects within task composition chain.

```typescript
const task = Task.fromPromise(someAsyncFunction())
  .tap((result) => {
    setSomething(result); // side-effects
  });

task.cancel(); // prevent unwanted side effects
```

### Chain multiple async functions togeter

In this scenario if one cancels the task during first operation, the seconf one would not be initiated at all

```typescript
const task = Task.fromPromise(someAsyncFunction())
  .chain((result1) => otherAsyncFunction(result1))
  .tap((result2) => {
    setSomething(result); // side-effects
  });

task.cancel(); // prevent unwanted side effects
```

### Chain multiple async functions via generator

In this scenario is identical to the previous one except of using generator syntax. Note usage of ```.generator()``` is required due to typescript being unable to predict types correctly othewise.

```typescript
const task = Task.generate(function*() {
  const result1 = yield* Task.fromPromise(someAsyncFunction()).generator();

  const result2 = yield* Task.fromPromise(otherAsyncFunction(result1)).generator();
  
  setSomething(result2); // side-effects
});

task.cancel(); // prevent unwanted side effects
```

### Throw exceptions straight to generators

It is also possible to reject pending tasks manually, resulting in an immediate interruption with exception. However you still can catch that exception within the task itself and fallback if necessary.

```typescript
const task = Task.generate(function*() {
  let result1: number;

  try {
    result1 = yield* Task.promiseGenerator(someAsyncFunction()); // equal to Task.fromPromise(<...>).generator();
  } catch (e) {
    console.error(e);

    result1 = 42; // Sometimes we have to give an answer in time i guess
  }

  const result2 = yield* Task.promiseGenerator(otherAsyncFunction(result1)); // equal to Task.fromPromise(<...>).generator();
  
  setSomething(result2); // side-effects
});

task.reject(new Error('Stop right there')); // prevent unwanted side effects
```

---
_Source: https://npm.io/package/t-tasks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
