Skip to content

Latest commit

 

History

History
40 lines (27 loc) · 1.29 KB

merge-signals.md

File metadata and controls

40 lines (27 loc) · 1.29 KB

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.

mergeSignals(...signals)

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);
});

signals

Type: AbortSignal, AbortController

The signals to listen to. If you pass a controller, it will automatically extract its signal.

Demo