Closing iterators #838

Closed
rauschma opened this Issue Feb 19, 2015 · 14 comments

Projects

None yet

3 participants

@rauschma

In the latest spec, for-of (and others) close iterators (by calling someIterator.return()).

function* foo() {
  yield 'a';
  yield 'b';
}
foo.prototype.return = function (...args) {
  console.log('RETURN');
  return super.return(...args);
};
for (let x of foo()) {
  console.log(x);
  break; // should trigger `return` method
}
@kittens
Member
kittens commented Feb 19, 2015

Should be easy enough.

@kittens
Member
kittens commented Feb 19, 2015

Although I'll have to make it match this too:

foo: for (let x of foo()) {
  console.log(x);
  break foo; // should trigger `return` method
}
@kittens kittens closed this in 8065c98 Feb 21, 2015
@kittens
Member
kittens commented Feb 21, 2015

Done, also handles labels perfectly.

@kittens
Member
kittens commented Feb 21, 2015

Released as of 4.4.0, thanks!

@zloirock
Member
@kittens
Member
kittens commented Feb 21, 2015

@zloirock Oh jeez, weird.

@kittens
Member
kittens commented Feb 21, 2015

@zloirock Easy fix, thanks! 61ea720

@zloirock
Member

@sebmck iterators may not have this method, see IteratorClose

@kittens
Member
kittens commented Feb 21, 2015

@zloirock Fixed.

@rauschma

Note that any kind of abrupt completion leads to return() being called (including exceptions!).

@kittens
Member
kittens commented Feb 21, 2015

Not sure if wrapping the entire block in a try-catch is a good idea.

@rauschma

Yes, not an easy decision.

@kittens
Member
kittens commented Feb 21, 2015

I'll push out a patch that fixes the issues @zloirock raised and I'll consider the viability of wrapping it in a try-catch although it doesn't look good.

@kittens kittens referenced this issue in google/traceur-compiler Feb 26, 2015
Closed

Can we avoid try catch with forOf? #1773

@kittens
Member
kittens commented Feb 27, 2015

for...of loops now close their iterator on abrupt completions. In loose mode this isn't the case.

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