New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

break-async-iterator

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

break-async-iterator

Break async iterator

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

break-async-iterator

Problem:

const neverResolves /*(without user input)*/ = process.stdin /* (Readable is an async iterator as of Node v10) */
async function* stuck() {
  for await (const i of neverResolves) {
    yield // never reaches
  }
}
let i = stuck()
i.next() // stuck
i.return() // still stuck

Solution

const breakAI = require('break-async-iterator')

const notStuck = breakAI(breakable => async function*() {
  try {
    for await (const i of breakable(neverResolves)) {
      yield
    }
  } finally {
    // reaches when `i.return()` or `i.throw()` is called
  }
})
let i = notStuck()
i.next() // still stuck
i.return() // but at least this can break the for-await loop

Install

npm i break-async-iterator

Usage

API

breakAI(breakable => async function* asyncGenerator(){...})

  • breakable A function that takes an iterator or a promise and returns a breakable version of the same

  • Returns An async generator that returns an iterator (wrapped around the one returned by asyncGenerator()) that's able to interrupt any iterators or promises (that were passed through breakable()) when its .return() or .throw() are called

Example

If you have an async generator function like this:

async function* asyncGenerator() {
  for await (const value of anotherAsyncIterator) {
    yield value
  }
  yield await aPromise
}

Then simply wrap it like this

const asyncGenerator = breakAI(breakable => async function*() {
  // and wrap any inner asyncIterators or promises with breakable:
  for await (const value of breakable(anotherAsyncIterator)) {
    yield value
  }
  yield await breakable(aPromise)
})

FAQs

Package last updated on 21 Aug 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc