-
Notifications
You must be signed in to change notification settings - Fork 34
forEach return value #217
Comments
It's inconsistency with |
However, since they have no way to pass a value at the end of the iteration, maybe it's acceptable. |
The final value upon iterating any of those types is |
Iterator helpers explicitly (and intentionally) do not forward the arguments to or result from Since the value from calling |
I agree with that behavior and believe that is coherent with what I’m proposing. More importantly, the iterator protocol currently does provide a vessel for observing the return value of a generator (and generator-like iterations like Agoric’s notifier streams) but currently the only way to observe that value is to hand-roll the iterator protocol. To the iterator protocol’s credit, this allowed us to implement I posit that, if the language provided any mechanism to observe the final value, it would be either the value of a I bring this up now because at Agoric we have a concrete case where a hypothetical |
Given that, maybe it would make more sense for the Generator prototype to override the
I suspect not, actually, at least for the first few years - I doubt many people would be relying on getting I don't think this is obviously a bad idea to include, but will need to think more about it.
With this proposed change, that would just be |
Bearing in mind that In the way that await Number.range(100).forAwaitEach(async job => {
// some async work to perform serially
}); Or, for bounded concurrency (10) on some (1000) jobs: const jobs = Number.range(1000); // assuming iterator, not iterable
await Promise.all(Number.range(10).map(() => jobs.forAwaitEach(async job => {
// some async work to perform in one of the 10 concurrent workers
})); Suffice it to say, I’m really excited about how expressive the standard library is getting when some of the in-flight proposals mature. |
Ah, I think you want await Number.range(100).toAsync().forEach(async job => {
// some async work to perform serially
}); will do what you want, I believe. |
That would suffice. |
|
Alternatively, |
@ptomato |
In the rest methods from this proposal, the final value is ignored. Why it should not be ignored in |
I'm not totally sure that it shouldn't be ignored in |
I agree that It might be appropriate to forward the final iteration result from To reïterate my argument for exposing the final value, the iterator protocol has a final value that is currently vestigial unless you code directly to the protocol. Unrolling the protocol is cumbersome and in my opinion, |
At the presentation to committee, there was no support for this change and 1 delegate preferred the status quo to match Array.prototype. Closing. |
As written today, the return value of
forEach
is undefined. I believe this would miss an opportunity to return thevalue
from the final{done: true, value}
iteration result of the underlying iteration such that:The text was updated successfully, but these errors were encountered: