Created
July 22, 2020 15:06
-
-
Save tteggel/a2770e77fb57cf924b3f1bdc3d4d0ad7 to your computer and use it in GitHub Desktop.
Take n items as a batch from an asyncIterator until done
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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