A utility library for working with AbortSignal in JavaScript and TypeScript.
npm install abort-signal


!npm bundle size

_abort-signal_ is a utility library for working with AbortSignal in JavaScript and TypeScript. It provides a modern, intuitive API to simplify common cancellation and timeout patterns.
---
- Installation
- Usage
- Timeout
- Combining Signals
- From an Event
- Documentation
- Contributing
- License
``bash`
pnpm install abort-signal
Install using your favorite package manager
npm
`bash`
npm install abort-signal
yarn
`bash`
yarn add abort-signal
The most common use case: aborting an operation if it takes too long.
`typescript`
try {
// Request will fail if it takes longer than 3 seconds.
const response = await fetch(url, { signal: Abort.timeout(3000) });
} catch (error) {
if (error instanceof TimeoutError) {
console.log('Request timed out!');
}
}
Abort an operation from one of several sources, like a timeout OR a user click.
`typescript
const userClickSignal = Abort.fromEvent(cancelButton, 'click');
const timeoutSignal = Abort.timeout(10000);
// Aborts on user click OR timeout, whichever comes first.
const combinedSignal = Abort.any([userClickSignal, timeoutSignal]);
await longRunningOperation({ signal: combinedSignal });
`
Bridge the DOM/event world with the cancellation world.
`typescript``
// Signal will abort as soon as the user navigates away or closes the tab.
const signal = Abort.fromEvent(window, 'unload');
await saveDraftToServer({ signal });
For detailed API documentation on all methods, please see the API docs.
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! 🙏
MIT © Shahrad Elahi and contributors.