Skip to content

Instantly share code, notes, and snippets.

@psenger
Created March 3, 2020 01:25
Show Gist options
  • Save psenger/6050f3309372a659e37dc44be5c7a12d to your computer and use it in GitHub Desktop.
Save psenger/6050f3309372a659e37dc44be5c7a12d to your computer and use it in GitHub Desktop.
[Example of Limiting Concurrency using ES6 iterator] #JavaScript
/*
Reference: https://stackoverflow.com/questions/40639432/what-is-the-best-way-to-limit-concurrency-when-using-es6s-promise-all
[Symbol.iterator]() is equivalent to .values()
const iterator = [1,2,3][Symbol.iterator]()
*/
const iterator = [1,2,3].values()
// loop over all items with for..of
for (const x of iterator) {
console.log('x:', x)
// notices how this loop continues the same iterator
// and consumes the rest of the iterator, making the
// outer loop not logging any more x's
for (const y of iterator) {
console.log('y:', y)
}
}
/**
We can use the same iterator and share it across workers.
If you had used .entries() instead of .values() you would have goten a 2D array with [index, value] which i will demonstrate below with a concurrency of 2
**/
const sleep = n => new Promise(rs => setTimeout(rs,n))
async function doWork(iterator) {
for (let [index, item] of iterator) {
await sleep(1000)
console.log(index + ': ' + item)
}
}
const arr = Array.from('abcdefghij')
const workers = new Array(2).fill(arr.entries()).map(doWork)
// ^--- starts two workers sharing the same iterator
Promise.all(workers).then(() => console.log('done'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment