buffered-async-iterable
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

Buffered processing of async iterables / generators in parallel to achieve comparable performance to Promise.all()

npm version npm downloads Module type: ESM Types in JS js-semistandard-style Follow @voxpelli@mastodon.social

WORK IN PROGRESS – early prerelease

Usage

Simple

import { bufferedAsyncMap } from 'buffered-async-iterable';

async function * asyncGenerator() {
  yield ...
}

const mappedIterator = bufferedAsyncMap(asyncGenerator(), async (item) => {
  // Apply additional async lookup / processing
});

for await (const item of mappedIterator) {
  // Consume the buffered async iterable
}

Array input

import { bufferedAsyncMap } from 'buffered-async-iterable';

const mappedIterator = bufferedAsyncMap(['foo'], async (item) => {
  // Apply additional async lookup / processing
});

for await (const item of mappedIterator) {
  // Consume the buffered async iterable
}

Async generator result

import { bufferedAsyncMap } from 'buffered-async-iterable';

const mappedIterator = bufferedAsyncMap(['foo'], async function * (item) => {
  // Apply additional async lookup / processing
  yield ...
  yield * ...
});

for await (const item of mappedIterator) {
  // Consume the buffered async iterable
}

API

bufferedAsyncMap(input, callback[, { bufferSize=6 }]) => AsyncIterableIterator

Iterates and applies the callback to up to bufferSize items from input yielding values as they resolve.

  • input – either an async iterable, an ordinare iterable or an array
  • callback(item) – should be either an async generator or an ordinary async function. Items from async generators are buffered in the main buffer and the buffer is refilled by the one that has least items in the current buffer (input is considered equal to sub iterators in this regard when refilling the buffer)

Options

  • bufferSizeoptional – defaults to 6, sets the max amount of simultanoeus items that processed at once in the buffer.

Similar modules

Package Sidebar

Install

npm i buffered-async-iterable

Weekly Downloads

2,227

Version

0.3.0

License

MIT

Unpacked Size

19.7 kB

Total Files

15

Last publish

Collaborators

  • voxpelli