@hazae41/signals
v1.0.2
Published
Utilities to deal with AbortSignal
Downloads
108
Maintainers
Readme
Signals
Utilities to deal with AbortSignal
npm i @hazae41/signals
Features
Current features
- 100% TypeScript and ESM
- No external dependencies
- Merge signals
- Optional signals
- Promisify signals
Usage
Never
Signals.never()
This can be useful when you want to accept an optional AbortSignal without branching or doing weird merging
function run(signal: AbortSignal);
// GOOD EXAMPLE
function f(signal = Signals.never()) {
run(signal)
}
// BAD EXAMPLE
function g(signal?: AbortSignal) {
if (signal == null)
run(new AbortController().signal)
else
run(signal)
}
Merge
Signals.merge(a, b?)
This can be useful when you have signals with different scopes
const global = AbortSignal.timeout(10 * 1000)
async function f(parent?: AbortSignal) {
const signal = Signals.merge(global, parent)
await fetch("...", { signal })
}
resolveOnAbort / rejectOnAbort
Signals.rejectOnAbort(signal)
This is very useful when you want to race a signal with a promise
async function run();
async function runWithTimeout(delay: number) {
const signal = AbortSignal.timeout(delay)
using timeout = Signals.rejectOnAbort(signal)
await Promise.race([run(), timeout.get()])
}
This converts any event-based signal into a disposable promise
You can also use resolveOnAbort
to avoid storing unhandled promises
async function run();
const global = AbortSignal.timeout(10 * 1000)
using timeout = Signals.resolveOnAbort(global)
async function runWithTimeout() {
const rejectOnAbort = timeout.get().then(() => { throw new Error("Aborted") })
await Promise.race([run(), rejectOnAbort])
}