hasNext() for ES6 Generator
The simple non-for…of
way to loop an iterator is
for (let iterator = range(1, 10), r; !(r = iterator.next()).done; ) {
console.log(r.value);
}
If you really want to use hasNext
, you can do that as well, but it's a bit weird:
const iterator = range(1, 10);
iterator.hasNext = function hasNext() {
const r = this.next();
this.current = r.value;
return !r.done;
};
while (iterator.hasNext()) {
console.log(iterator.current);
}