GithubHelp home page GithubHelp logo

happy-ferret / abortable-iterator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alanshaw/abortable-iterator

0.0 2.0 0.0 272 KB

Make any iterator or iterable abortable via an AbortSignal

License: MIT License

JavaScript 100.00%

abortable-iterator's Introduction

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

Name Type Description
iterator Iterable|Iterator The iterator or iterable object to make abortable
signal AbortSignal Signal obtained from AbortController.signal which is used to abort the iterator.
options Object (optional) options
options.onAbort Function An (async) function called when the iterator is being aborted, before the abort error is thrown. Default null
options.abortMessage String The message that the error will have if the iterator is aborted. Default "operation aborted"
options.abortCode String|Number The value assigned to the code property of the error that is thrown if the iterator is aborted. Default "ERR_ABORTED"

Returns

Type Description
Iterator An iterator that wraps the passed iterator parameter that makes it abortable via the passed signal parameter.

The returned iterator will throw an Error when it is aborted that has a code property with the value ERR_ABORTED by default.

Contribute

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

License

MIT © Alan Shaw

abortable-iterator's People

Contributors

alanshaw avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.