Socket
Book a DemoInstallSign in
Socket

abortable-iterator

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abortable-iterator

Make any iterator or iterable abortable via an AbortSignal

Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
9.3K
37.61%
Maintainers
1
Weekly downloads
 
Created
Source

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(iterator, signal, [options])

Make any iterator or iterable abortable via an AbortSignal.

Parameters

NameTypeDescription
iteratorIterable|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"

Returns

TypeDescription
IteratorAn iterator that wraps the passed iterator 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.

Contribute

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

License

MIT © Alan Shaw

Keywords

async

FAQs

Package last updated on 18 Apr 2019

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