mergeiterator
Advanced tools
Comparing version 1.2.3 to 1.2.4
{ | ||
"name": "mergeiterator", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "merges async iterators", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -9,12 +9,7 @@ # mergeiterator | ||
### Using | ||
Merges list of async or sync iterables into async one. | ||
This utility is for merging together async iterators. | ||
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. | ||
```js | ||
merge(collection_of_iterables): Iterable | ||
``` | ||
Pass it a collection of iterables and it'll return an [async iterator](https://github.com/tc39/proposal-async-iteration), which will contain all values from those iterables. Those iterables and the collection of them can be arrays, calls to generators, or any other kind of iterable, synchronous or async, finite or infinite. | ||
```javascript | ||
@@ -24,24 +19,29 @@ import merge from "mergeiterator" | ||
async function DoIt() { | ||
const array = [1,2,3] | ||
const array = [1, 2, 3, 4, 5] | ||
const promisedArray = Promise.resolve([6, Promise.resolve(7)]) | ||
function *generator() { | ||
let i = 6 | ||
let i = 10 | ||
while (true) yield (i++) | ||
}) | ||
} | ||
async function *asyncGenerator() { | ||
yield await Promise.resolve(4) | ||
yield Promise.resolve(5) | ||
}) | ||
for await (const v of merge([array, generator(), asyncGenerator()])) { | ||
yield 8 | ||
yield Promise.resolve(9) | ||
} | ||
for await (const v of merge([array, promisedArray, generator(), asyncGenerator()])) { | ||
console.log(v) | ||
} | ||
} | ||
// 1 6 2 7 4 3 8 5 9 10 11 ... | ||
// 1 2 6 3 7 10 4 11 8 5 12 9 13 14 15 ... | ||
``` | ||
`mergeIterator` function guarantees, that if some value is yielded by some of iterables that `mergeIterator` is passed, then that value will be eventually yielded by `mergeIterator`. It also guarantees that the order of values within the same iterable is preserved. | ||
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 yielded, not a promise itself. | ||
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 | ||
@@ -62,4 +62,4 @@ | ||
- `sequences` **AnyIterable<AnyIterable<([Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<Yield> | Yield), any>, Return>** | ||
- `sequences` **AnyIterable<AnyIterable<([Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<T> | T)>, ReturnT>** | ||
Returns **$AsyncIterator<Yield, Return, void>** | ||
Returns **AsyncGenerator<T, ReturnT, void>** |
@@ -5,3 +5,3 @@ // @flow | ||
type AnyIterable<Yield, Return> = $AsyncIterable<Yield, Return, void> | $Iterable<Yield, Return, void> | ||
type AnyIterable<T, ReturnT = *> = $AsyncIterable<T, ReturnT, void> | $Iterable<T, ReturnT, void> | ||
@@ -11,5 +11,3 @@ /** | ||
*/ | ||
export async function* merge<Yield, Return>( | ||
sequences: AnyIterable<AnyIterable<Promise<Yield> | Yield, *>, Return>, | ||
): $AsyncIterator<Yield, Return, void> { | ||
export async function* merge<T, ReturnT>(sequences: AnyIterable<AnyIterable<Promise<T> | T>, ReturnT>): AsyncGenerator<T, ReturnT, void> { | ||
const rootIterator = getIterator(sequences) | ||
@@ -16,0 +14,0 @@ const readers = [readRootIterator] |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
352