proposal-async-iterator-helpers icon indicating copy to clipboard operation
proposal-async-iterator-helpers copied to clipboard

Should `.drop`'s dropped items be read in parallel?

Open bakkot opened this issue 2 years ago • 4 comments

Consider:

asyncIteratorOfUrls
  .map(url => fetch(url))
  .drop(3)
  .next();

Should the first four fetches (the three dropped, and then one which will be returned) happen in parallel, or in sequence?

That is, should the first call to .next on the .drop(N) helper be like

let promises = [];
for (let i = 0; i < N; ++i) {
  promises.push(inner.next());
}
let actualNext = inner.next();
return Promise.all(promises).then(
  ps => ps.some(x => x.done) ? ({ done: true }) : actualNext,
  err => ({ done: true }),
);

or

try {
  for (let i = 0; i < N; ++i) {
    let { done } = await inner.next();
    if (done) return { done: true };
  }
} catch (e) {
  return { done: true };
}
return inner.next();

? (Some details of error handling omitted.)

I am inclined to say the former (they should be parallel), but that implies maybe we need a mechanism to for async functions to limit their degree of parallelism (like in this npm package).

bakkot avatar Feb 05 '23 04:02 bakkot