
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
async-cancelator
Advanced tools
A minimal async control and cancellation library for JavaScript and TypeScript
A minimal, zero-dependency library for managing asynchronous tasks with built-in support for cancellation and timeout management.
npm install async-cancelator
import { createCancellable } from 'async-cancelator';
const { promise, cancel } = createCancellable(async (signal) => {
// Check if cancelled during async operations
if (signal.cancelled) return;
// Long running operation...
await someAsyncOperation();
// Check again if cancelled
if (signal.cancelled) return;
return 'Operation completed';
});
// Later, if needed:
cancel('Operation no longer needed');
import { createCancellableWithReject, CancellationError } from 'async-cancelator';
const { promise, cancel } = createCancellableWithReject(async (signal) => {
// No need to check signal.cancelled as the promise will be rejected
// Long running operation...
await someAsyncOperation();
return 'Operation completed';
});
try {
// Later, if needed:
cancel('Operation no longer needed');
const result = await promise;
// Handle result
} catch (error) {
if (error instanceof CancellationError) {
// Handle cancellation
console.log(`Operation was cancelled: ${error.message}`);
} else {
// Handle other errors
}
}
import { withTimeout, TimeoutError } from 'async-cancelator';
// Automatically rejects after 5000ms
const timeoutPromise = withTimeout(
fetch('https://api.example.com/data'),
5000,
'Request timed out'
);
try {
const result = await timeoutPromise;
// Handle result
} catch (error) {
if (error instanceof TimeoutError) {
// Handle timeout
console.log(`Operation timed out: ${error.message}`);
} else {
// Handle other errors
}
}
import { createCancellable, withTimeout } from 'async-cancelator';
const { promise, cancel } = createCancellable(async (signal) => {
// Your async operation
// Remember to check signal.cancelled at appropriate points
});
// Add timeout to a cancellable promise
const timeoutPromise = withTimeout(promise, 3000, 'Operation timed out');
// You can still cancel manually
setTimeout(() => cancel('No longer needed'), 1000);
import { useEffect, useState, useRef } from 'react';
import { createCancellableWithReject, TimeoutError, CancellationError } from 'async-cancelator';
function useFetchData(url) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const cancelRef = useRef(null);
useEffect(() => {
const fetchData = async () => {
const { promise, cancel } = createCancellableWithReject(async (signal) => {
const response = await fetch(url);
const data = await response.json();
return data;
});
// Store the cancel function for cleanup
cancelRef.current = cancel;
try {
const result = await promise;
setData(result);
setLoading(false);
} catch (error) {
if (error instanceof CancellationError) {
// Don't set error state for cancellations
} else {
setError(error);
setLoading(false);
}
}
};
fetchData();
// Cleanup function to cancel the operation when the component unmounts
return () => {
if (cancelRef.current) {
cancelRef.current('Component unmounted');
}
};
}, [url]);
return { data, loading, error };
}
createCancellable(fn)Creates a cancellable promise wrapper.
Parameters:
fn: Function that receives a cancellation signal and returns a PromiseReturns:
promise and cancel functioncreateCancellableWithReject(fn)Creates a cancellable promise wrapper that automatically rejects when cancelled.
Parameters:
fn: Function that receives a cancellation signal and returns a PromiseReturns:
promise and cancel functionwithTimeout(promise, ms, message)Adds a timeout to any promise.
Parameters:
promise: The promise to add a timeout toms: Timeout in millisecondsmessage: Optional message for the timeout errorReturns:
withTimeoutFn(fn, ms, message)Creates a function that adds a timeout to a promise-returning function.
Parameters:
fn: Function that returns a promisems: Timeout in millisecondsmessage: Optional message for the timeout errorReturns:
createCancellableWithTimeout(fn, ms, message)Creates a cancellable promise with a timeout.
Parameters:
fn: Function that receives a cancellation signal and returns a Promisems: Timeout in millisecondsmessage: Optional message for the timeout errorReturns:
promise and cancel functionCancellationError: Error thrown when a promise is cancelledTimeoutError: Error thrown when a promise times outMIT
FAQs
A minimal async control and cancellation library for JavaScript and TypeScript
We found that async-cancelator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.