tc39 / proposal-iterator-helpers

Methods for working with iterators in ECMAScript
https://tc39.es/proposal-iterator-helpers
1.33k stars 35 forks source link

stage 3 tracking #117

Closed michaelficarra closed 1 year ago

michaelficarra commented 4 years ago
michaelficarra commented 4 years ago

Things have gotten really busy for me and I don't think I'll be able to prepare this for stage 3 (at least not on my own) in time for the stage advancement deadline on 10th Sep.

Jack-Works commented 4 years ago

Maybe also wait for tc39/ecma262#2045 to merge

jorendorff commented 4 years ago

I think @avandolder is interested in helping. I can help too.

The fact that tc39/ecma262#2045 is stalled is a major obstacle. If there's anything I can do to help there, let me know! You can ping me on IRC any time.

Jack-Works commented 4 years ago

Hmm cause there's nothing to update with #2045... I didn't get a clue about what should I change to make it merged, or it just waiting for some editor to press the merge button?

bakkot commented 4 years ago

The editors need to review it, which will take a while because it's a substantial change.

michaelficarra commented 4 years ago

@jorendorff That PR is on the top of my list for review. I'll hopefully get to it this week, possibly even today. An additional review from you (even if it just results in an approval) would be helpful.

mpcsh commented 4 years ago

I'm not sure how I missed this, but I will unfortunately not be able to provide the stage 3 review I committed to at the previous plenary. my sincere apologies to the champions and stakeholders. I will be doubly careful to ensure I stay on top of review commitments going forward.

michaelficarra commented 4 years ago

@mpcsh That's fine, we will ask for a new one at the meeting next week: https://github.com/tc39/agendas/commit/868028cbc63ddee53a04ca88cfe07ac154377ed0

michaelficarra commented 4 years ago

Update: @codehag has volunteered (possibly to be replaced in the future by @jorendorff).

benjamingr commented 3 years ago

Hey - are there any updates on this proposal? When do you expect to present it again?

(I am asking because Node.js has started to integrate async iterators in many places and this would be very useful for Node!)

karlhorky commented 3 years ago

@benjamingr out of interest, can you link to an example or two of how Node is using iterators in the codebase?

Also eagerly awaiting this proposal!

benjamingr commented 3 years ago

@karlhorky

But mostly streams and all stream helpers (like pipeline), the easiest way to get a Node.js stream is to use Readable.from with an async generator.

The "new way" of writing Node.js is getting rather pervasive:

I came here because I really wanted a setInterval(1000).map(someFn) and we had a discussion about this.

benjamingr commented 3 years ago

And to clarify - I think it's safe to say Node.js is very interested in this proposal and since a bunch of our use cases arose from web platform APIs I am sure a very compelling case can be made for it in browsers too.

devsnek commented 3 years ago

This proposal was originally made by me and Domenic, so I do hope our respective ecosystems find it useful 😅

Jack-Works commented 3 years ago

Though I'm happy to see iterator helpers and usage of for await of but I want to mention it is really easy to get into the performance trap (write serial code when it can be parallel, just like the misuse on await).

for await (const req of connection) {
    // easy to write but actually wrong in some cases
    req.response(await longTimeAsyncIO(req))
    // oops, actually turn handling into one-by-one,
    // and if it fails it actually block all future connections
}

for await (const req of connection) {
    try { longTimeAsyncIO(req).then(req.response.bind(req)) } catch {}
}
benjamingr commented 3 years ago

@Jack-Works I believe the whole point of “for await of” with async iterators is to serialize code so it appears sync yet works with backpressure (not requesting more items from the iterator ahead of time).

Making async code look and behave serially is the major selling point of async/await for me.

It is very easy to just consume and process several bits from the iterator at once (call .next several times without waiting for the first one to fulfill).

Also I suggest we move this discussion elsewhere as it does not relate to this proposal’s status :)

rektide commented 3 years ago

Just mentioning, although not yet stage 3, we have seen some implementation begin (flagged), in Firefox, according to the README.

benjamingr commented 3 years ago

That’s great thanks for the update :)

michaelficarra commented 2 years ago

@gibson042 @ljharb @rbuckton This proposal is ready for review. Note the 3 open issues which will be resolved by the remaining open PRs depending on committee decision when this is presented for stage 3. Please review those PRs in addition to the current spec text.

michaelficarra commented 2 years ago

Reminder @gibson042 @ljharb @rbuckton that this proposal is potentially up for stage 3 at the next meeting. The slides have been posted to the agenda. Please review this proposal ahead of the meeting.

ljharb commented 2 years ago

Spec review:

rbuckton commented 2 years ago

Do we normally have headings as deep as "3.1.2.2.2.1.1"? The formatting for the headers looks off for https://tc39.es/proposal-iterator-helpers/#sec-wrapforvaliditeratorprototype-object and a few others.

michaelficarra commented 2 years ago

@rbuckton The section numbers are correct. It's not terribly unusual. See 14.7.5.10.2.1, 16.2.1.5.1.1, 16.2.1.5.2.1, 16.2.1.5.2.2, 16.2.1.5.2.3, 16.2.1.5.2.4, 16.2.1.5.2.5.

michaelficarra commented 2 years ago

@ljharb

  • "filterer" is a weird argument name. why not "predicate"?

  • can the argument be named "predicate" instead of "fn"?

Sure, though I'll wait on wait on the resolution to #211 first. In the slides, which assume #211 is merged, I've named them predicateWithCounter.

rbuckton commented 2 years ago
rbuckton commented 2 years ago

The spec doesn't currently define what behavior a "built-in async function" has.

I'd be fine with the wording of the scalar methods on AsyncIterator.prototype if the proposal spec clearly defined how algorithm steps for a "built-in async function" should work. Just adding an Await isn't sufficient to consider this complete specification text.

Methods like Iterator.prototype.filter get around this hand-wavy-ness by specifying an Abstract Closure and passing it to CreateIteratorFromClosure, which handles all of the iterator scaffolding and explicitly calls out that Yield shorthand is safe to use.

bakkot commented 2 years ago

Re: handling of NaN, see https://github.com/tc39/proposal-iterator-helpers/issues/169. This was also mentioned during the presentation in July.

bakkot commented 2 years ago

Iterator.prototype.reduce Step 3.b - I find it surprising that reduce will throw if there is no initialValue and the iterator is empty, since Array.prototype.reduce does not throw.

[].reduce(x => x) does in fact throw. Am I not understanding the comparison?

AsyncIterator.prototype.flatMap Step 3.a.vi and viii - We do a lot of awaiting and async iterator wrapping here. I just want to verify that the intended return value from the mapper is something like Promise<AsyncIterable | Iterable> | AsyncIterable | Iterable.

Yup. It's gross, but consistent - await expects Promise<T> | T, and for await expects AsyncIterable<T> | Iterable<T>, so if you're iterating the result of a potentially-async function you compose them and get Promise<AsyncIterable<T> | Iterable<T>> | AsyncIterable<T> | Iterable<T>.

AsyncIterator.prototype.reduce Steps 1, 2, and 3.b - Since this method is intended to return a Promise, should we not be throwing this error as a promise rejection?

See https://github.com/tc39/proposal-iterator-helpers/issues/218 for the under-specification issue. I believe the intent of "built-in async function" is that they behave like normal async functions, i.e. all ThrowCompletions are wrapped up in promise rejections.

michaelficarra commented 2 years ago
  • This does an OrdinaryHasInstance check for %Iterator%, but does not check whether the iterator has a next() method, while GetIteratorDirect does perform a check for a valid next method. As a result, its possible to use Iterator.from on a subclass of Iterator that does not define next, but not on a regular object that does not define next. Is that intended?

@rbuckton I think you're confused here. The OrdinaryHasInstance test is done in the path that is supporting Iterables. Are you thinking of a case where an object implements Symbol.iterator and returns something that inherits from Iterator.prototype but then also has a non-callable next own-property?

rbuckton commented 2 years ago

Are you thinking of a case where an object implements Symbol.iterator and returns something that inherits from Iterator.prototype but then also has a non-callable next own-property?

Yes, but also any class that subclasses Iterator, since %IteratorPrototype% also has a [Symbol.iterator] method.

class BadIterator extends Iterator {}
const iter = Iterator.from(new BadIterator()); // succeeds
typeof iter.next === "undefined";

const iter2 = Iterator.from({}); // throws TypeError because there's no `next` method.
rbuckton commented 2 years ago

Is the proposal's %Iterator.prototype% replacing %IteratorPrototype%? In https://tc39.es/proposal-iterator-helpers/#sec-well-known-intrinsic-objects it seems to be referred to in both ways, but I can't find any examples of similar aliasing in the main spec.

rbuckton commented 2 years ago

[].reduce(x => x) does in fact throw. Am I not understanding the comparison?

Ah no, I was scanning the steps for reduce and jumped to step 8 and missed the check on step 4.

michaelficarra commented 2 years ago

Is the proposal's %Iterator.prototype% replacing %IteratorPrototype%? In tc39.es/proposal-iterator-helpers/#sec-well-known-intrinsic-objects it seems to be referred to in both ways, but I can't find any examples of similar aliasing in the main spec.

It's just an alternative way to reference it. In the well-known intrinsic objects section, we allow this kind of reference via

A reference such as %name.a.b% means, as if the "b" property of the value of the "a" property of the intrinsic object %name% was accessed prior to any ECMAScript code being evaluated.

rbuckton commented 2 years ago

Yes, but no other entry in the table is structured the way its structured in the proposal. All of the %XPrototype% entries in that table are listed individually because there's no way to directly access them by dotting off of something as you quoted. Having both %IteratorPrototype% and %Iterator.prototype% in the spec seems unnecessarily confusing (as evidenced by the fact I had to ask for clarification), but is something that can be addressed in the actual ecma262 PR.

But this goes to my original point. The algorithm steps in Iterator.from and AsyncIterator.from don't reliably check for next and assume any iterable whose iterator inherits from %Iterator.prototype% (or %AsyncIterator.prototype%) is safe to use when that's not necessarily the case.

If you're ok with that, then that's fine. But I wanted to make sure that was an intended consequence rather than something that was overlooked.

ljharb commented 2 years ago

In the actual PR, we wouldn't list %IteratorPrototype% at all, we'd list %Iterator%, and we'd change any mention of %IteratorPrototype% in 262, 402, or HTML to use %Iterator.prototype%.

rbuckton commented 2 years ago

It's also worth noting that the behavior of Iterator.from(obj) will differ if obj inherits from an %Iterator.prototype% from another realm: Iterator.from(new class extends Iterator {}) will not check for next, but Iterator.from(new class extends someIFrame.contentWindow.Iterator {}) will check for next.

michaelficarra commented 2 years ago

@gibson042 @ljharb @rbuckton Please do another review. You may want to review each of the recently merged PRs individually, or you can review the spec text as a whole. I think the section most in need of review is the new built-in async functions section (and associated machinery) added in https://github.com/tc39/proposal-iterator-helpers/pull/240. GetIteratorFlattenable could also possibly use another set of eyes.

I plan to ask for stage 3 at the upcoming November meeting.

michaelficarra commented 1 year ago

Note to reviewers about the built-in async functions:

I've opened #245 which removes that infrastructure from this proposal and instead relies on https://github.com/tc39/ecma262/pull/2942 to add it. You can still review that PR if you like, but you should no longer feel obligated to do so. Now you can focus on just the parts that are relevant to this proposal.

michaelficarra commented 1 year ago

Fixed by #251.