1
  • Promise.all only resolves when all promises resolve, rejects when the first promise rejects

  • Promise.allSettled only resolves once all promises settle

  • Promise.any resolves only the first resolved promise, rejects when all the promises reject

  • Promise.race settles with the same result of the first promise to settle

It seems that Promise in Javascript does not have the equivalent of asyncio.as_completed which yields Promises in the order they settle.

Is it possible to achieve the same logic of asyncio.as_completed with user code?

Mehdi Saffar
  • 691
  • 1
  • 8
  • 24
  • This is an exact duplicate of: [Array of Promises resolve Promises as soon as completed?](https://stackoverflow.com/questions/45092395/array-of-promises-resolve-promises-as-soon-as-completed) – Kevin B May 18 '23 at 18:54
  • @KevinB disagree: in python that function *executes* the coros and returns await-ables, in JS the Promise code runs as soon as it's constructed and Promise.all turns the container inside out, array of promise -> promise of array. AFAICT there is no equivalent JS semantic, or to be more precise there's no *need* for that function in JS, but I don't think that makes it a dupe of the other. I could be wrong. – Jared Smith May 18 '23 at 18:57
  • 2
    I think you're looking for [async generator yielding promise results as they are resolved](https://stackoverflow.com/q/70044213/1048572), does that the same as `as_completed` in python? – Bergi May 18 '23 at 20:28
  • The [python docs](https://docs.python.org/3/library/asyncio-task.html#asyncio.as_completed) mention that "*Generators yielding tasks are not accepted*" for some reason. If you'd like something that can handle even that, see [Unordered resolution of a list of promises](https://stackoverflow.com/q/74293770/1048572) – Bergi May 18 '23 at 20:36

0 Answers0