# @yume-chan/async

> Utils for async programming.

Latest version **4.1.3** (published 2025-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @yume-chan/async
pnpm add @yume-chan/async
yarn add @yume-chan/async
bun add @yume-chan/async
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

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

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.3 |
| Published | 2025-04-17 |
| First published | 2021-01-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Simon Chan |
| Maintainers | yume-chan |
| Keywords | async, util |

## Links

- npm: https://www.npmjs.com/package/@yume-chan/async
- Repository: https://github.com/yume-chan/async
- Homepage: https://github.com/yume-chan/async#readme
- Issues: https://github.com/yume-chan/async/issues
- npm.io page: https://npm.io/package/@yume-chan/async

## Recent versions

- 4.1.3 (latest) — 2025-04-17
- 4.1.2 — 2025-04-04
- 4.1.1 — 2025-03-22
- 4.1.0 — 2025-03-22
- 4.0.2 — 2024-11-19
- 4.0.1 — 2024-11-19
- 4.0.0 — 2024-10-23
- 2.2.0 — 2022-06-12
- 2.1.4 — 2021-01-29

## README

# Async

![license](https://img.shields.io/npm/l/@yume-chan/async)
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/yume-chan/async/Node.js%20CI?label=build)
![Codecov](https://img.shields.io/codecov/c/github/yume-chan/async?token=Z6KEN9JL2A)
[![npm version](https://img.shields.io/npm/v/@yume-chan/async)](https://www.npmjs.com/package/@yume-chan/async)
![npm type definitions](https://img.shields.io/npm/types/@yume-chan/async)

Utils for async programming.

- [PromiseResolver](#promiseresolver)
- [AsyncOperationManager](#asyncoperationmanager)

## PromiseResolver

``` ts
export type PromiseResolverState = 'running' | 'resolved' | 'rejected';

export class PromiseResolver<T> {
    readonly promise: Promise<T>;
    readonly state: PromiseResolverState;

    resolve(value?: T | PromiseLike<T>): void;
    reject(reason?: any): void;
}
```

C#, the origin of the async/await pattern, uses [`TaskCompletionSource`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskcompletionsource-1) to manage `Task`s manually.

V8, the most commonly used JavaScript engine, uses `PromiseResolver` to manage `Promise`s manually.

But in JavaScript, we only have the `Promise` constructor, and there is no way to know the `Promise`'s state.

Now you can use this `PromiseResolver` class to create and manage `Promise`s more conveniently.

``` ts
function delay(timeout: number): Promise<void> {
    const resolver = new PromiseResolver<void>();
    setTimeout(() => resolver.resolve(), timeout);
    return resolver.promise;
}
```

## AsyncOperationManager

``` ts
export interface AsyncOperationInfo<T> {
    id: number;
    promise: Promise<T>;
}

export default class AsyncOperationManager {
    add<T>(): AsyncOperationInfo<T>;

    resolve<T>(id: number, result: T): void;
    reject(id: number, reason: Error): void;
}
```

Assume you have an RPC service, every operation has an ID, and the remote will return the result with this ID.

`AsyncOperationManager` can help you manage the IDs and convert callbacks to `Promise`s.

``` ts
declare const MyService;
const manager = new AsyncOperationManager();

MyService.on('complete', (id: number, result: number) => {
    manager.resolve(id, result);
});

MyService.on('error', (id: number, message: string) => {
    manager.reject(id, new Error(message));
});

function callService(payload: number): Promise<number> {
    const { id, promise } = manager.add<number>();
    MyService.post({ id, payload });
    return promise;
}
```

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