Coder Social home page Coder Social 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() {
	const array = [1, 2, 3, 4, 5]
	const promisedArray = Promise.resolve([6, Promise.resolve(7)])
	function *generator() {
		let i = 10
		while (true) yield (i++)
	}
	async function *asyncGenerator() {
		yield 8
		yield Promise.resolve(9)
	}
	for await (const v of merge([array, promisedArray, generator(), asyncGenerator()])) {
		console.log(v)
	}
}

// 1 2 6 3 7 10 4 11 8 5 12 9 13 14 15 ...

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.

The return value of merge is the return value of the list of iterables. Return values of merged iterables are discarded.

API

Table of Contents

merge

Merges async or sync iterables into async one.

Parameters

  • sequences AnyIterable<AnyIterable<T>, ReturnT>

Returns AsyncGenerator<T, ReturnT, void>

mergeiterator's People

Contributors

dependabot[bot] avatar vadzim 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.