Note
This utility is available natively as AbortSignal.any()
. Chrome has supported it since August 2023, Node since June 2023 and Safari/Firefox since March 2024. Prefer using the native method if you don't need to support older browsers.
Returns an AbortSignal
that aborts when any of the input is aborted. Ideal when:
- you want to add a timeout signal
- you receive a
signal
and want to have your own internal abort controller.
import {mergeSignals} from 'abort-utils';
// First signal
const userAction = new AbortController();
cancelButton.addEventListener('click', () => {
userAction.abort('User cancelled');
});
// Second signal
const timeout = AbortSignal.timeout(100);
// Merged signal
const mergedSignal = mergeSignals(timeout, userAction.signal);
mergedSignal.addEventListener('abort', () => {
console.log('One of the signals was aborted', mergedSignal.reason);
});
Type: AbortSignal
, AbortController
The signals to listen to. If you pass a controller, it will automatically extract its signal.