Skip to content

Instantly share code, notes, and snippets.

@reu
Created February 17, 2022 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reu/d49c9e45138ed6909db5418f20236376 to your computer and use it in GitHub Desktop.
Save reu/d49c9e45138ed6909db5418f20236376 to your computer and use it in GitHub Desktop.
Async iterator that yields each promises results in resolve order
async function* promisesIter(promises) {
promises = [...promises];
const buffer = [];
for (promise of promises) {
promise.then(result => {
promises.splice(promises.indexOf(promise), 1);
buffer.push(result);
});
}
while (promises.length === 0 || await Promise.race(promises).then(() => true)) {
while (buffer.length > 0) yield buffer.pop();
if (promises.length === 0) break;
await new Promise(res => setTimeout(res, 0));
}
}
@dfkaye
Copy link

dfkaye commented Feb 18, 2022

This is so freaking awesome, I made a copy with comments and some renaming, etc., but also added an inline test.

https://gist.github.com/dfkaye/1260506378b71dac62596acc54174a97

@dfkaye
Copy link

dfkaye commented Feb 21, 2022

@reu I found a couple things that needed changing:

  1. the yield should use shift() instead push() so the buffer acts like a queue not a stack, and
  2. the await timeout should appear as the first not the last step in the loop (else not all results are buffered).

I’ve updated my gist with more detail, and I promise to stop bugging you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment