tc39 / proposal-iterator-helpers

Methods for working with iterators in ECMAScript

Home Page:https://tc39.es/proposal-iterator-helpers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Do other checks before `GetIteratorDirect`

kmiller68 opened this issue · comments

Right now the current way the spec is phrased makes it impossible to polyfill/implement the proposal in JS as a for-of loop. Since you'd like to do something like:

function* map(callback) {
  if (!isCallable(callback))
    throw new TypeError();

  for (let item of this)
    yield callback(item); 
}

but the current spec prohibits such an implementation because it opens the iterator before checking for is callable and doing something like:

function* map(callback) {
  let didChecks = false;

  for (let item of this) {
    if (!didChecks) {
      didChecks = true;
      if (!isCallable(callback))
        throw new TypeError();
    }
    yield callback(item); 
  }
}

still doesn't work because it pumps the iterator once before checking if the callback is callable. Could we change the spec to move the Let iterated be ? [GetIteratorDirect](https://tc39.es/proposal-iterator-helpers/#sec-getiteratordirect)(this value). line of each method next to the appropriate Let next be ? [IteratorStep](https://tc39.es/ecma262/#sec-iteratorstep)(iterated). line?

Hm, yeah. I also think it would be more consistent with the rest of the spec to do all of the validation (of this and the arguments) before we start reading from this. I agree we should change these.

Opened #265. Assuming other people are on board with this change we'll ask for consensus for it in March.

I should note that it even with this tweak it's non-trivial to polyfill with a for-of because iterator helpers use this.next directly, rather than using this[Symbol.iterator]().next as a for-of would.