UNPKG

1.19 kBJavaScriptView Raw
1import { wrapWithAbort } from './operators/withabort';
2import { AsyncIterableX } from './asynciterablex';
3import { returnAsyncIterator } from '../util/returniterator';
4import { throwIfAborted } from '../aborterror';
5export class ZipAsyncIterable extends AsyncIterableX {
6 constructor(sources) {
7 super();
8 this._sources = sources;
9 }
10 // eslint-disable-next-line consistent-return
11 async *[Symbol.asyncIterator](signal) {
12 throwIfAborted(signal);
13 const sourcesLength = this._sources.length;
14 const its = this._sources.map((x) => wrapWithAbort(x, signal)[Symbol.asyncIterator]());
15 while (sourcesLength > 0) {
16 const values = new Array(sourcesLength);
17 for (let i = -1; ++i < sourcesLength;) {
18 const { value, done } = await its[i].next();
19 if (done) {
20 await Promise.all(its.map(returnAsyncIterator));
21 return undefined;
22 }
23 values[i] = value;
24 }
25 yield values;
26 }
27 }
28}
29export function zip(...sources) {
30 return new ZipAsyncIterable(sources);
31}
32
33//# sourceMappingURL=zip.mjs.map