UNPKG

1.28 kBJavaScriptView Raw
1import { AsyncIterableX } from '../asynciterable';
2import { bindCallback } from '../util/bindcallback';
3class FlatMapAsyncIterable extends AsyncIterableX {
4 constructor(source, selector) {
5 super();
6 this._source = source;
7 this._selector = selector;
8 }
9 async *[Symbol.asyncIterator]() {
10 for await (const outer of this._source) {
11 for await (const inner of this._selector(outer)) {
12 yield inner;
13 }
14 }
15 }
16}
17/**
18 * Projects each element of a sequence to a potentially async iterable and flattens the
19 * resulting sequences into one sequence.
20 * @param {Iterable<T | Promise<T>> | AsyncIterable<T>} source Source sequence
21 * @param {function:(value: T): Iterable<R | Promise<R>> | AsyncIterable<R>} selector A transform function to apply to each element.
22 * @param {Object} [thisArg] An optional "this" binding for the selector function.
23 * @returns {AsyncIterable<R>} An async iterable whose elements are the result of invoking the one-to-many
24 * transform function on each element of the input sequence.
25 */
26export function flatMapAsync(source, selector, thisArg) {
27 return new FlatMapAsyncIterable(source, bindCallback(selector, thisArg, 1));
28}
29
30//# sourceMappingURL=flatmapasync.mjs.map