What is abort-controller?
The abort-controller npm package provides a way to abort one or more Web API tasks, such as fetch requests, by using an AbortController object. This package is a polyfill for the AbortController interface based on the WHATWG Fetch specification, which allows you to cancel tasks by calling the abort() method on the controller instance.
What are abort-controller's main functionalities?
Creating an AbortController instance
This feature allows you to create a new instance of AbortController, which can be used to generate a signal that can be passed to fetch or other APIs that support aborting.
const AbortController = require('abort-controller');
const controller = new AbortController();
const signal = controller.signal;
Aborting a fetch request
This code sample demonstrates how to use the AbortController to abort an ongoing fetch request. The signal is passed to the fetch call, and the request can be aborted by calling the abort() method on the controller.
const fetch = require('node-fetch');
const AbortController = require('abort-controller');
const controller = new AbortController();
const signal = controller.signal;
fetch('https://example.com', { signal })
.then(response => response.json())
.catch(err => {
if (err.name === 'AbortError') {
console.log('Fetch aborted');
} else {
console.error('Fetch error:', err);
}
});
// Abort the request
controller.abort();
Other packages similar to abort-controller
p-cancelable
The p-cancelable package provides a way to create cancelable promises. It is similar to abort-controller in that it allows you to cancel asynchronous tasks, but it works by wrapping promises rather than using a signal-based approach.
bluebird
Bluebird is a fully-featured promise library that includes support for cancellation. It is more comprehensive than abort-controller as it provides a full promise implementation with additional features like map, reduce, and filter for promises, but it also includes the ability to cancel promises.
axios
Axios is a promise-based HTTP client for the browser and Node.js that includes a cancellation feature. While abort-controller is focused on providing an AbortController polyfill, axios offers a broader set of HTTP client features, including the ability to cancel requests using a CancelToken.
abort-controller
An implementation of WHATWG AbortController interface.
import AbortController from "abort-controller"
const controller = new AbortController()
const signal = controller.signal
signal.addEventListener("abort", () => {
console.log("aborted!")
})
controller.abort()
https://jsfiddle.net/1r2994qp/1/
💿 Installation
Use npm to install then use a bundler.
npm install abort-controller
Or download from dist
directory.
📖 Usage
Basic
import AbortController from "abort-controller"
const AbortController = require("abort-controller")
const AbortController = window.AbortControllerShim
If your bundler recognizes browser
field of package.json
, the imported AbortController
is the native one and it doesn't contain shim (even if the native implementation was nothing).
If you wanted to polyfill AbortController
for IE, use abort-controller/polyfill
.
Polyfilling
Importing abort-controller/polyfill
assigns the AbortController
shim to the AbortController
global variable if the native implementation was nothing.
import "abort-controller/polyfill"
require("abort-controller/polyfill")
API
AbortController
https://dom.spec.whatwg.org/#interface-abortcontroller
controller.signal
The AbortSignal object which is associated to this controller.
controller.abort()
Notify abort
event to listeners that the signal
has.
📰 Changelog
🍻 Contributing
Contributing is welcome ❤️
Please use GitHub issues/PRs.
Development tools
npm install
installs dependencies for development.npm test
runs tests and measures code coverage.npm run clean
removes temporary files of tests.npm run coverage
opens code coverage of the previous test with your default browser.npm run lint
runs ESLint.npm run build
generates dist
codes.npm run watch
runs tests on each file change.