Skip to content

wtgtybhertgeghgtwtg/buffer-async-iterator

Repository files navigation

buffer-async-iterator

Buffer an AsyncIterator before iteration.

Create an AsyncIterator over the values of an AsyncIterator, eagerly iterated.

Install

$ yarn buffer-async-iterator

API

Table of Contents

bufferAsyncIterator

Create an AsyncIterator from iterable, with the values eagerly buffered.

Parameters

  • iterable (AsyncIterable<Value> | Iterable<Value>) The iterable whose values will be buffered.
  • write Write A function that takes iterable and buffer and returns a Promise. buffer will not be iterated through until it resolves. (optional, default defaultWrite)
  • buffer (AsyncIterable<Value> | Iterable<Value>) An iterable representing the data being buffered. (optional, default [])

Examples

import bufferAsyncIterator from 'bufferAsyncIterator';
import got from 'got';

async function* getWebsites(sites) {
  for (const site of sites) {
    const {requestUrl} = got.head(site);
    yield requestUrl;
  }
}

// Immediate start running the generator.
const websiteIterator = bufferAsyncIterator(getWebsites(['google.com', 'bing.com', 'yahoo.com']));

// Other potentially time-consuming stuff here.

// Iteration begins only after the original generator has finished.
for await (const site of websiteIterator) {
  console.log(site);
}

License

MIT © Matthew Fernando Garcia

About

Buffer an AsyncIterator before iteration.

Resources

License

Stars

Watchers

Forks

Packages

No packages published