fast-abort-controller
v4.0.4
Published
fast abort controller implementation that can be used as a ponyfill/polyfill
Downloads
5,513
Maintainers
Readme
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