fast-abort-controller
TypeScript icon, indicating that this package has built-in type declarations

4.0.4 • Public • Published

FastAbortController

fast abort controller implementation that can be used as a ponyfill/polyfill

Installation

npm i --save fast-abort-controller

Usage

Supports both ESM and CommonJS

// esm
import AbortController from 'fast-abort-controller'
// commonjs
const AbortController = require('fast-abort-controller').default

Example with Fetch (which supports abort signals)

import AbortController from 'fast-abort-controller'

const controller = new AbortController()

const signal = controller.signal

function cancel() {
  signal.abort()
}

try {
  await fetch('codeshare.io', { signal })
  console.log('request success')
} catch (err) {
  if (err.name === 'AbortError') {
    console.warn('request aborted')
  } else {
    console.error('request error', err)
  }
}

// ...

Example with raceAbort

import { readFile } from 'fs/promises'

import AbortController from 'fast-abort-controller'
import raceAbort from 'race-abort'

const controller = new AbortController()

const signal = controller.signal

function cancel() {
  signal.abort()
}

try {
  // readFile doesnt support cancellation so we wrap it with raceAbort provided by another module 'race-abort'
  await raceAbort(readFile('foo/bar/qux'), signal)
  console.log('request success')
} catch (err) {
  if (err.name === 'AbortError') {
    console.warn('request aborted')
  } else {
    console.error('request error', err)
  }
}

License

MIT

Package Sidebar

Install

npm i fast-abort-controller

Weekly Downloads

25

Version

4.0.4

License

MIT

Unpacked Size

31.1 kB

Total Files

39

Last publish

Collaborators

  • tjmehta