# race-signal
[](https://codecov.io/gh/achingbrain/race-signal)
[](https://github.com/achingbrain/race-signal/actions/workflows/js-test-and-release.yml?query=branch%3Amain)
> Race a promise against an AbortSignal
# About
Pass a promise and an abort signal and await the result.
## Example - Basic usage
```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)
```
## Example - Overriding errors
By default the thrown error is the `.reason` property of the signal but it's
possible to override this behaviour with the `translateError` 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!')`
const resolve = await raceSignal(promise, controller.signal, {
translateError: (signal) => {
// use `signal`, or don't
return new Error('Oh no!')
}
})
```
# Install
```console
$ npm i race-signal
```
## Browser `
```
# API Docs
-
# License
Licensed under either of
- Apache 2.0, ([LICENSE-APACHE](https://github.com/achingbrain/race-signal/LICENSE-APACHE) / )
- MIT ([LICENSE-MIT](https://github.com/achingbrain/race-signal/LICENSE-MIT) / )
# Contribution
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.