3

I have a promise array with a 100 promises. I want to await until a single promise fulfills with a truthy value. Also, I want to make sure only x promises run concurrently (set the limit).

I've looked into methods like bluebird.any and bluebird.some but they don't do have a concurrency option.

How do I do this?

Elmo
  • 6,409
  • 16
  • 72
  • 140
  • 2
    promises don't run. A promise is just the future result of some code executed. – Jonas Wilms May 02 '19 at 10:04
  • 2
    Check this one: https://www.npmjs.com/package/p-limit – blaz May 02 '19 at 10:05
  • For waiting for a single promise is it a single particular promise or any of the concurrent promises? if it is any promise.race is something you can look into along with p-limit library above. – Shyam Babu May 02 '19 at 10:07
  • Check there: https://stackoverflow.com/questions/40639432/what-is-the-best-way-to-limit-concurrency-when-using-es6s-promise-all – Evaldas May 02 '19 at 10:17
  • Check out [iter-ops](https://github.com/vitaly-t/iter-ops), which has operator [waitRace](https://vitaly-t.github.io/iter-ops/functions/waitRace.html), so you can remap a sequence into promises, and then control how many promises run at a time. – vitaly-t Dec 04 '22 at 13:56

1 Answers1

0

Bluebird promise.map should solve your issue along with the option of concurrency.

promise.map(promiseArray, {concurrency: 4})

The concurrency here basically limits the number of promises running state. If you make concurrency to 1, then they execute serially.

Sreehari
  • 1,328
  • 11
  • 29
  • This does indeed solve the concurrency and I was aware of it. But it waits for all the promises to resolve. I just need to wait until one of them resolves with a truthy value. – Elmo May 02 '19 at 10:24