1.0.0 • Published 4 years ago
iterate-all v1.0.0
iterate-all
A utility function that converts any of these:
Iterable<T>Iterable<Promise<T>>AsyncIterable<T>AsyncIterable<Promise<T>>
Into this:
Promise<Array<T>>
Usage
import {iterateAll} from "iterate-all";
async function* someAsyncGenerator() {
yield 1;
yield Promise.resolve(2);
yield 3;
}
const results = await iterateAll(someAsyncGenerator());
console.log(results); // [1, 2, 3];Notes
- Each yielded value will be
awaited before asking for the next value. As such, all execution will proceed serially; the same as if usingfor..oforfor await..of. - The
nextfunction on the iterable will always be called with no arguments. As such, when using a generator function to create your iterable, the result of anyyieldexpression inside the generator function will always be undefined. - If you find yourself wanting a synchronous version of this,
Array.from(potentially combined withPromise.all) is probably sufficient for your needs.
License
MIT
1.0.0
4 years ago