Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created April 25, 2017 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/306ef1fda6525fe8474fd4e89fe4121d to your computer and use it in GitHub Desktop.
Save mattpodwysocki/306ef1fda6525fe8474fd4e89fe4121d to your computer and use it in GitHub Desktop.
'use strict';
import { isAsyncIterable } from '../internal/isasynciterable';
export async function *flattenAsync<T>(iterable: AsyncIterable<T>, depth: number = Infinity): AsyncIterable<T> {
if (depth === 0) { return iterable; }
for await (let item of iterable) {
if (isAsyncIterable(item)) {
yield *flattenAsync(item, depth - 1);
} else {
yield item;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment