Race a promise against an AbortSignal
npm install race-signal

> Race a promise against an AbortSignal
Pass a promise and an abort signal and await the result.
``ts
import { raceSignal } from 'race-signal'
const controller = new AbortController()
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('a value')
}, 1000)
})
setTimeout(() => {
controller.abort()
}, 500)
// throws an AbortError
const resolve = await raceSignal(promise, controller.signal)
`
By default the thrown error is the .reason property of the signal but it'stranslateError
possible to override this behaviour with the option:
`ts
import { raceSignal } from 'race-signal'
const controller = new AbortController()
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('a value')
}, 1000)
})
setTimeout(() => {
controller.abort()
}, 500)
// throws Error('Oh no!')signal
const resolve = await raceSignal(promise, controller.signal, {
translateError: (signal) => {
// use , or don't`
return new Error('Oh no!')
}
})
`console`
$ npm i race-signal
-
Licensed under either of
- Apache 2.0, (LICENSE-APACHE /
- MIT (LICENSE-MIT /
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.