I'm looking for the best way to make an async iterator in Javascript. I'm already using generators and async/await functions, but I can't find a way to achieve the so-called for-await-of loop without using the latest Node.js release (10.x).
The problem is the following :
I'm fetching large amount of data from an API and I need to scroll the content of it. I don't want to store all the content in an array, as it would explode in RAM.
I know how to do that using streams, but it would be cooler (to my point of view) to do it using generators.
Thanks for your help,
Best regards,
-- Corentin
Async iterators are not available in node.js below v10, but there are some pretty good ways to consume a stream asynchronously without iterators, for example with scramjet
:
const stream = DataStream.from(someNodeObjectStream);
await (async () => {
while (let data = await stream.whenRead()) {
doSomethingWith(data);
}
})();
This is more or less the syntactic sugar you get from async iterators.