Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@smithy/abort-controller
Advanced tools
@smithy/abort-controller is a package that provides an implementation of the AbortController interface, which is used to signal that an operation should be aborted. This is particularly useful for managing and controlling asynchronous operations, such as HTTP requests, in a more efficient manner.
Creating an AbortController
This feature allows you to create an instance of AbortController and obtain its associated signal. The signal can then be passed to any operation that supports aborting.
const { AbortController } = require('@smithy/abort-controller');
const controller = new AbortController();
const signal = controller.signal;
Aborting an Operation
This feature demonstrates how to use the AbortController to abort an ongoing asynchronous operation. The operation checks if the signal is aborted and listens for the abort event to handle the abortion.
const { AbortController } = require('@smithy/abort-controller');
const controller = new AbortController();
const signal = controller.signal;
// Simulate an asynchronous operation
const fetchData = (signal) => {
return new Promise((resolve, reject) => {
if (signal.aborted) {
return reject(new Error('Operation aborted'));
}
setTimeout(() => resolve('Data fetched'), 1000);
signal.addEventListener('abort', () => reject(new Error('Operation aborted')));
});
};
fetchData(signal).catch(err => console.error(err.message));
// Abort the operation after 500ms
setTimeout(() => controller.abort(), 500);
The 'abort-controller' package is a popular implementation of the AbortController interface. It provides similar functionality to @smithy/abort-controller, allowing you to create and manage abort signals for asynchronous operations. It is widely used and well-documented.
While 'axios' is primarily an HTTP client, it has built-in support for request cancellation using AbortController. This makes it a good choice if you need both HTTP request capabilities and abort functionality in one package.
The 'node-fetch' package is a lightweight module that brings window.fetch to Node.js. It supports AbortController for aborting fetch requests, making it a good alternative for handling HTTP requests with abort capabilities.
FAQs
A simple abort controller library
The npm package @smithy/abort-controller receives a total of 19,016,913 weekly downloads. As such, @smithy/abort-controller popularity was classified as popular.
We found that @smithy/abort-controller demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 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
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.