race-signal
Race a promise against an AbortSignal
Last updated 8 months ago by achingbrain .
Apache-2.0 OR MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install race-signal 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 2.0.0                                ...           latest (8 months ago)

10 Versions

  • 2.0.0                                ...           8 months ago
  • 1.1.3                                ...           a year ago
  • 1.1.2                                ...           a year ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)

Copyright 2013 - present © cnpmjs.org | Home |