abort-signal
v1.0.0
Published
A utility library for working with AbortSignal in JavaScript and TypeScript.
Maintainers
Readme
abort-signal
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
pnpm install abort-signalnpm
npm install abort-signalyarn
yarn add abort-signal📖 Usage
Timeout
The most common use case: aborting an operation if it takes too long.
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!');
}
}Combining Signals
Abort an operation from one of several sources, like a timeout OR a user click.
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 });From an Event
Bridge the DOM/event world with the cancellation world.
// Signal will abort as soon as the user navigates away or closes the tab.
const signal = Abort.fromEvent(window, 'unload');
await saveDraftToServer({ signal });📚 Documentation
For detailed API documentation on all methods, please see the API docs.
🤝 Contributing
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! 🙏
License
MIT © Shahrad Elahi and contributors.
