Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

race-signal

achingbrain417.3kApache-2.0 OR MIT2.0.0TypeScript support: included

Race a promise against an AbortSignal

readme

race-signal

codecov CI

Race a promise against an AbortSignal

About

Pass a promise and an abort signal and await the result.

Example - Basic usage

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:

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

$ npm i race-signal

Browser <script> tag

Loading this module through a script tag will make its exports available as RaceSignal in the global namespace.

<script src="https://unpkg.com/race-signal/dist/index.min.js"></script>

API Docs

License

Licensed under either of

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.

changelog

2.0.0 (2025-08-19)

⚠ BREAKING CHANGES

  • signal.reason is now thrown and you must use translateError to throw a custom error

Bug Fixes

  • throw signal.reason, remove error fields (#79) (88bb875)

1.1.3 (2025-02-23)

Bug Fixes

1.1.2 (2025-02-21)

Bug Fixes

  • do not leave the promise dangling when the signal is aborted (#64) (6333fbc)

1.1.1 (2025-02-21)

Trivial Changes

Dependencies

  • dev: bump aegir from 44.1.4 to 45.0.8 (#63) (b2bd93e)

1.1.0 (2024-08-06)

Features

1.0.3 (2024-08-06)

Trivial Changes

Dependencies

  • dev: bump aegir from 41.3.5 to 44.1.0 (#57) (2067ab6)

1.0.2 (2023-11-16)

Bug Fixes

1.0.1 (2023-10-06)

Bug Fixes

  • improve stack trace of thrown error (#45) (ae387a8)

Dependencies

  • dev: bump aegir from 40.0.13 to 41.0.0 (#44) (ce1688e)

1.0.0 (2023-09-24)

⚠ BREAKING CHANGES

  • this module is now ESM-only

Features

Trivial Changes

0.1.0 (2021-12-21)