
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
node-abort-controller
Advanced tools
AbortController Polyfill for Node.JS based on EventEmitter for Node v14.6.x and below.
Are you using Node 14.7.0 or above? You don't need this! Node has AbortController and AbortSignal as builtin globals. In Node versions >=14.7.0 and <15.4.0 you can access the experimental implementation using --experimental-abortcontroller.
fetchimport fetch from "node-fetch";
import { AbortController } from "node-abort-controller";
const controller = new AbortController();
const signal = controller.signal;
await fetch("https:/www.google.com", { signal });
// Abort fetch after 500ms. Effectively a timeout
setTimeout(() => controller.abort(), 500);
fetch function with a built in timeoutimport { AbortController } from "node-abort-controller";
import fetch from "node-fetch";
const fetchWithTimeout = async (url = "") => {
const controller = new AbortController();
const { signal } = controller;
const timeout = setTimeout(() => {
controller.abort();
}, 5000);
const request = await fetch(url, { signal });
clearTimeout(timeout);
const result = await req.json();
return result;
};
You might not need to! Generally speaking, there are three environments your JavaScript code can run in:
For modern JS APIs, each environment would ideally get a polyfill:
In practice, this is hard. Tooling such as webpack and browserify are great at making sure stuff works out of the box in all environments. But it is quite easy to fail on both points above. In all likelyhood, you end up shipping less than ideal polyfills on platforms that don't even need them. So what is a developer to do? In the case of fetch and AbortController I've done the work for you. This is a guide to that work.
If you are building a ...
You don't need this library! AbortController is now built into nodeJS . Use that instead.
You don't need a library! Close this tab. Uninstall this package.
Use this package and node-fetch. It is minimally what you need.
Use abort-controller and whatwg-fetch. These are more complete polyfills that will work in all browser environments.
Use abort-controller and cross-fetch. Same as above, except cross-fetch will polyfill correctly in both the browser and node.js
fetch internallyUse this package and node-fetch. It is the smallest and least opinionated combination for your end users. Application developers targeting Internet Exploer will need to polyfill AbortController and fetch on their own. But your library won't be forcing unecessary polyfills on developers who only target modern browsers.
With the above guide in mind, this library has a very specific set of goals:
This is the ideal for library authors who use fetch and AbortController internally and target both browser and node developers.
Thank you @mysticatea for https://github.com/mysticatea/abort-controller. It is a fantastic AbortController polyfill and ideal for many use cases.
The 'abort-controller' package is another implementation of the AbortController interface. It provides similar functionality to 'node-abort-controller' and can be used to abort fetch requests or other asynchronous operations.
The 'p-cancelable' package allows you to create cancelable promises. Unlike 'node-abort-controller', which implements the standard AbortController interface, 'p-cancelable' provides a custom API for creating and canceling promises.
The 'cancelable-promise' package is another alternative that provides the ability to cancel promises. It offers a different API compared to 'node-abort-controller' and is not based on the AbortController interface.
FAQs
AbortController for Node based on EventEmitter
The npm package node-abort-controller receives a total of 10,326,396 weekly downloads. As such, node-abort-controller popularity was classified as popular.
We found that node-abort-controller demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.