Giter Site home page Giter Site logo

mergeiterator's Introduction

mergeiterator

Merges async iterators.

NPM version Build Status Coverage Status

Merges list of async or sync iterables into async one.

It accepts any iterable like arrays, generators, async generators or even promises which resolve to iterables. Any iterator can be infinite, including the list of iterables itself.

import merge from "mergeiterator"

async function DoIt() {
	for await (const v of merge([
		[1, 2, Promise.resolve(3)],
		Promise.resolve([4, 5]),
		(function*() {
			let i = 9
			while (true) {
				yield i++
				yield Promise.resolve(i++)
			}
		})(),
		(async function*() {
			yield 6
			yield await Promise.resolve(7)
			yield Promise.resolve(8)
		})(),
	])) {
		console.log(v)
	}
}

// 1 4 2 9 5 3 10 6 11 7 12 8 13 14 15 16 17 18 19 20 ...

This function guarantees, that if some value is yielded by some of iterables, then that value will be eventually yielded. This is basically about infinite iterables. It also guarantees that the order of values within the same iterable is preserved.

If some iterable yields a promise, its value will be used, not a promise itself.

If some iterable throws an error, that error will be redirected to a caller and other iterables will be closed.

Return values of iterables are discarded.

API

merge

Merges async or sync iterables into async one.

Parameters

  • sequences AnyIterable<AnyIterable<T>>

Returns AsyncGenerator<T, any, any>

mergeiterator's People

Contributors

vadzim avatar dependabot[bot] avatar

Stargazers

David Wells avatar Víctor Fernández avatar kryptish avatar Markus Felten avatar Geoff Goodman avatar Jason Holloway avatar Niklas Salmoukas avatar Alex avatar Greg Reimer avatar laggingreflex avatar

Watchers

 avatar James Cloos avatar  avatar

mergeiterator's Issues

merge() does not propagate iteration end to the children iterators

Example code:

import delay from "delay";
import merge from "mergeiterator";

async function* iterable(name: string, dt: number) {
  try {
    for (let i = 0; ; i++) {
      console.log(`${name}: ${i}`);
      yield `${name}: ${i}`;
      await delay(dt);
    }
  } finally {
    console.log(`Exited ${name}`);
  }
}

async function* caller() {
  //yield* iterable("A", 900);
  yield* merge(iterable("A", 900));
}

async function main() {
  for await (const message of caller()) {
    if (message.includes("4")) {
      break;
    }

    console.log(`Received ${message}`);
  }

  console.log("Finishing");
  await delay(3000);
}

main().catch((e) => console.log(e));

In this example I do a "dummy" merging of 1 iterable for simplicity (but we can merge multiple, the effect persists). The output is:

A: 0
Received A: 0
A: 1
Received A: 1
A: 2
Received A: 2
A: 3
Received A: 3
A: 4
Finishing

Notice that finally {} block in iterable() function was never executed. But it should: replace the call to merge() with yield* iterable("A", 900); to see the correct output (with "Exited A"):

A: 0
Received A: 0
A: 1
Received A: 1
A: 2
Received A: 2
A: 3
Received A: 3
A: 4
Exited A
Finishing

How it works: both for-await and yield* instructions call the source iterator's return() method once the loop is over, and they propagate that signal further up the stack. I think merge() just doesn't do this.

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.