https://github.com/can-acar/bun-cancellation-token
Lightweight cancellation tokens built on top of AbortController for Bun/TypeScript.
https://github.com/can-acar/bun-cancellation-token
bun typescript
Last synced: about 1 month ago
JSON representation
Lightweight cancellation tokens built on top of AbortController for Bun/TypeScript.
- Host: GitHub
- URL: https://github.com/can-acar/bun-cancellation-token
- Owner: can-acar
- License: mit
- Created: 2025-10-23T10:55:40.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-10-23T13:11:16.000Z (8 months ago)
- Last Synced: 2025-10-23T15:14:07.164Z (8 months ago)
- Topics: bun, typescript
- Language: TypeScript
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bun-cancellation-token
Lightweight cancellation tokens built on top of AbortController for Bun/TypeScript.
## Install
```bash
bun add bun-cancellation-token
```
## Usage
```ts
import { CancellationTokenSource, abortableDelay } from "bun-cancellation-token";
const src = new CancellationTokenSource();
// cancel after 50ms
setTimeout(() => src.cancel("timeout"), 50);
try {
await abortableDelay(500, src.token);
} catch (err) {
console.log("aborted:", err);
}
```
## Scripts
- Dev: runs the entry module locally
```bash
bun run dev
```
- Test: runs unit tests with Bun's test runner
```bash
bun test
```
- Build: bundles to `dist/` for Bun target
```bash
bun run build
```
This package is designed primarily as a Bun module and ships TypeScript sources for first-class Bun support.
## Bun API Examples
Run these examples to see integration with Bun runtime APIs:
- Fetch with timeout (AbortSignal from token passed to fetch)
```bash
bun run ex:fetch
```
- Simple HTTP server with per-request timeouts and graceful shutdown
```bash
bun run ex:serve
```
- Concurrency with linked tokens (overall deadline cancels slower tasks)
```bash
bun run ex:concurrency
```