Trying to wrap my head around generator and I just don't get it.
I have a simple non generator iterator class called KeyGen that I would like to rewrite using JS generators:
If this isn't what generators are suppose to do than please leave a comment and I'll close
class KeyGen {
constructor(numOfKeys) {
this.keys = new Array(numOfKeys).fill(0).map((i, index) => index);
this.iteratorCount = 0;
}
keyIterator() {
return {
next: () => this.keys[this.iteratorCount++]
}
}
}
const keyGen = new KeyGen(4);
console.log(keyGen.keyIterator().next());
console.log(keyGen.keyIterator().next());
console.log(keyGen.keyIterator().next());
console.log(keyGen.keyIterator().next());