Skip to content

Instantly share code, notes, and snippets.

@tteggel
Created July 22, 2020 15:06
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 tteggel/a2770e77fb57cf924b3f1bdc3d4d0ad7 to your computer and use it in GitHub Desktop.
Save tteggel/a2770e77fb57cf924b3f1bdc3d4d0ad7 to your computer and use it in GitHub Desktop.
Take n items as a batch from an asyncIterator until done
async function* take(n, obj) {
let done;
const iterator = obj[Symbol.asyncIterator]();
while(!done) {
yield Promise.all(
Array.from(
{length: n},
async function() {
const result = await this.next();
done = result.done;
return result.value;
},
iterator
)
);
}
}
// example
// useful for controlling parallelism
// take 10 at a time from asyncIterableObject to work on in parallel, wait until all 10 items are complete
// before getting next batch.
for await (const batch of take(10, asyncIterableObject)) {
await Promise.all(
batch.map(async item => await item.doStuff())
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment