A collection of wrappers and utility functions for working with AbortSignals.
npm i @toebean/signals
Combines several AbortSignal
instances into a signal which will be aborted as soon as any of the given signals are.
import { AggregateSignal } from "@toebean/signals";
const ac = new AbortController();
const aggregateSignal = new AggregateSignal(ac.signal, someOtherSignal);
// passing an aggregate signal into an awaitable, abortable call:
await doSomeAbortableAction({ signal: aggregateSignal.signal });
// determining which of the original signals was aborted first:
switch (aggregateSignal.abortedSignal) {
case ac.signal:
// do stuff
break;
// etc...
}
Creates an AbortSignal
which will timeout after a given number of milliseconds, using setTimeout
under the hood.
import { AggregateSignal } from "@toebean/signals";
const timeoutSignal = new TimeoutSignal(200); // creates an AbortSignal which will abort in 200ms
// passing a timeout signal into an awaitable, abortable call:
await doSomeAbortableAction({ signal: timeoutSignal.signal });
// If for whatever reason you need to clear the underlying timeout of the TimeoutSignal, you can:
clearTimeout(timeoutSignal.timeout);
A TypeScript type guard for checking whether a given object is an AbortSignal
.
import { isAbortSignal } from "@toebean/signals";
if (isAbortSignal(someObject)) {
// within this block, someObject is typed as an AbortSignal
console.log(someObejct.aborted);
}
The full API reference for signals is available on GitHub Pages.
signals is licensed under MIT © 2022 Tobey Blaber.