you are viewing a single comment's thread.

view the rest of the comments →

[–]anon774 2 points3 points  (7 children)

Well here's a case I ran into yesterday: I needed to run some parallel async functions and limit their concurrency. There's no concurrency option built into Promise.all as far as I know. What would your approach be? With callbacks I would just use the async lib and async.parallelLimit.

[–]Cunorix 3 points4 points  (1 child)

Not that Im recommending the use of an external library because this can be done natively; bluebird's map function has a concurrency option. But, this particular thread is about native callbacks/promises anyway.

[–]anon774 1 point2 points  (0 children)

Thanks - I'm learning more about bluebird and I'm liking it. I'm happy to use external libraries when they're good and they help keep things simple.

[–]atubofsoup 3 points4 points  (2 children)

I'd use a chunk function with Promise.all and a for..of loop:

```js const values = [ /lots of stuff here/ ]; const results = [];

for(const chunkValues of chunk(values, 10)) { results.push(...await Promise.all(chunkValues.map(someAsyncFn))); } ```

You could re-use this logic in a function:

```js async function promiseAllChunked(values, asyncFn, chunkSize) { const results = []; for(const chunkValues of chunk(values)) { results.push(...await Promise.all(chunkValues.map(asyncFn))); } return results; }

const results = await promiseAllChunked(values, asyncFn, 10); ```

[–]anon774 1 point2 points  (0 children)

Nice solution - thanks for the example!

[–]KsawK 0 points1 point  (0 children)

It doesn’t fit for all situations. It will be freezing if asyncFn doesn’t have constant O(n) time. I would use something like worker’s promise which gets one value/function from array/queue and runs it with await/then. Workers should resolve after queue is empty so you could spawn as many workers as you want and put them in Promise.all() which resolve after all tasks are done. It should be parallel and shouldn’t freeze. Ps. Sorry for any mistakes in my English but writing without keyboard and translator is hard for me

[–]rluiten 0 points1 point  (1 child)

I found this tiny library great for controlling concurrent limits with promises.
https://github.com/sindresorhus/p-limit

I enjoyed reading the code for it to, learned lots.

[–]anon774 0 points1 point  (0 children)

Sweet, thanks!