Make any iterator or iterable abortable via an AbortSignal

Clone this repo:
  1. d1f1762 3.0.0 by Alan Shaw · 3 years, 1 month ago master v3.0.0
  2. c3c68a1 feat: add typescript typings...your mileage may vary, PRs accepted by Alan Shaw · 3 years, 1 month ago
  3. 6136d75 feat: add returnOnAbort option by Alan Shaw · 3 years, 3 months ago
  4. de71c9a feat: abortable duplex by Alan Shaw · 3 years, 3 months ago
  5. 7a7dd5a docs: add related section by Alan Shaw · 3 years, 3 months ago

abortable-iterator

Build Status dependencies Status JavaScript Style Guide

Make any iterator or iterable abortable via an AbortSignal

The AbortController is used in the fetch API to abort in flight requests from, for example, a timeout or user action. The same concept is used here to halt iteration of an async iterator.

Install

npm install abortable-iterator

Usage

const abortable = require('abortable-iterator')
const AbortController = require('abort-controller')

// An example function that creates an async iterator that yields an increasing
// number every x milliseconds and NEVER ENDS!
const asyncCounter = async function * (start, delay) {
  let i = start
  while (true) {
    yield new Promise(resolve => setTimeout(() => resolve(i++), delay))
  }
}

// Create a counter that'll yield numbers from 0 upwards every second
const everySecond = asyncCounter(0, 1000)

// Make everySecond abortable!
const controller = new AbortController()
const abortableEverySecond = abortable(everySecond, controller.signal)

// Abort after 5 seconds
setTimeout(() => controller.abort(), 5000)

try {
  // Start the iteration, which will throw after 5 seconds when it is aborted
  for await (const n of abortableEverySecond) {
    console.log(n)
  }
} catch (err) {
  if (err.code === 'ERR_ABORTED') {
    // Expected - all ok :D
  } else {
    throw err
  }
}

API

const abortable = require('abortable-iterator')

abortable(source, signal, [options])

(alias for abortable.source(source, signal, [options]))

Make any iterator or iterable abortable via an AbortSignal.

Parameters

NameTypeDescription
sourceIterable|IteratorThe iterator or iterable object to make abortable
signalAbortSignalSignal obtained from AbortController.signal which is used to abort the iterator.
optionsObject(optional) options
options.onAbortFunctionAn (async) function called when the iterator is being aborted, before the abort error is thrown. Default null
options.abortMessageStringThe message that the error will have if the iterator is aborted. Default "The operation was aborted"
options.abortCodeString|NumberThe value assigned to the code property of the error that is thrown if the iterator is aborted. Default "ABORT_ERR"
options.returnOnAbortBooleanInstead of throwing the abort error, just return from iterating over the source stream.

Returns

TypeDescription
IterableAn iterator that wraps the passed source parameter that makes it abortable via the passed signal parameter.

The returned iterator will throw an AbortError when it is aborted that has a type with the value aborted and code property with the value ABORT_ERR by default.

abortable(source, signals)

(alias for abortable.source(source, signals))

Make any iterator or iterable abortable via any one of the passed AbortSignal's.

Parameters

NameTypeDescription
sourceIterable|IteratorThe iterator or iterable object to make abortable
signalsArray<{ signal, [options] }>An array of objects with signal and optional options properties. See above docs for expected values for these two properties.

Returns

TypeDescription
IteratorAn iterator that wraps the passed source parameter that makes it abortable via the passed signal parameters.

The returned iterator will throw an AbortError when it is aborted on any one of the passed abort signals. The error object has a type with the value aborted and code property with the value ABORT_ERR by default.

abortable.sink(sink, signal, [options])

The same as abortable.source except this makes the passed sink abortable. Returns a new sink that wraps the passed sink and makes it abortable via the passed signal parameter.

abortable.sink(sink, signals)

The same as abortable.source except this makes the passed sink abortable via any one of the passed AbortSignal's. Returns a new sink that wraps the passed sink and makes it abortable via the passed signal parameters.

abortable.transform(transform, signal, [options])

The same as abortable.source except this makes the passed transform abortable. Returns a new transform that wraps the passed transform and makes it abortable via the passed signal parameter.

abortable.transform(transform, signals)

The same as abortable.source except this makes the passed transform abortable via any one of the passed AbortSignal's. Returns a new transform that wraps the passed transform and makes it abortable via the passed signal parameters.

abortable.duplex(duplex, signal, [options])

The same as abortable.source except this makes the passed duplex abortable. Returns a new duplex that wraps the passed duplex and makes it abortable via the passed signal parameter.

Note that this will abort both sides of the duplex. Use duplex.sink = abortable.sink(duplex.sink) or duplex.source = abortable.source(duplex.source) to abort just the sink or the source.

abortable.duplex(duplex, signals)

The same as abortable.source except this makes the passed duplex abortable via any one of the passed AbortSignal's. Returns a new duplex that wraps the passed duplex and makes it abortable via the passed signal parameters.

Note that this will abort both sides of the duplex. Use duplex.sink = abortable.sink(duplex.sink) or duplex.source = abortable.source(duplex.source) to abort just the sink or the source.

Related

  • it-pipe Utility to "pipe" async iterables together

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Alan Shaw