-
Reiter, Christoph authored
These allow linking multiple AbortControllers and ceate a timeout abort signal. For example in case you want to abort a fetch in case (1) the UI element gets removed (2) a newer request replacing this one gets started (3) a timeout happens because the fetch takes too long createLinkedAbortController() allows merging multiple signals into one and createTimeoutAbortSignal() allows creating a singal that auto aborts after some time passes.
Reiter, Christoph authoredThese allow linking multiple AbortControllers and ceate a timeout abort signal. For example in case you want to abort a fetch in case (1) the UI element gets removed (2) a newer request replacing this one gets started (3) a timeout happens because the fetch takes too long createLinkedAbortController() allows merging multiple signals into one and createTimeoutAbortSignal() allows creating a singal that auto aborts after some time passes.
abort.js 922 B
/**
* Takes multiple AbortSignal instances and returns a new AbortController which
* gets aborted if any of the AbortSignals do.
*
* @param {...AbortSignal} signals
* @returns {AbortController}
*/
export function createLinkedAbortController(...signals) {
const controller = new AbortController();
for (const signal of signals) {
if (signal.aborted) {
controller.abort();
break;
} else {
signal.addEventListener('abort', () => {
controller.abort();
});
}
}
return controller;
}
/**
* Returns an AbortSignal which aborts after the specified time.
*
* @param {number} delay Delay in milliseconds
* @returns {AbortSignal}
*/
export function createTimeoutAbortSignal(delay) {
const controller = new AbortController();
setTimeout(() => {controller.abort(); }, delay);
return controller.signal;
}