If we terminate a loop early, for-of
invokes method .return()
of the iterator. Generators return generator objects (that implement both the Iterable interface and the Iterator interface). If we invoke .return()
on such an object, the generator is terminated. We can prevent that by overriding .return()
.
More information: https://exploringjs.com/es6/ch_iteration.html#sec_iteration-protocol-in-depth
Demonstration:
function logIter(iter) {