Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

race-signal

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

race-signal

Race a promise against an AbortSignal

latest
Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
116K
-30.62%
Maintainers
1
Weekly downloads
 
Created
Source

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.

FAQs

Package last updated on 19 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts