Skip to content

Instantly share code, notes, and snippets.

@dwiyatci

dwiyatci/xgen-and-gen.js

Last active Apr 17, 2020
Embed
What would you like to do?
function* is not a gen.
function* xcounter() {
let index = 0;
while (true) {
index += 1;
const x = yield index;
console.log(x);
}
}
const xgen = xcounter();
console.log(xgen.next().value);
console.log(xgen.next(42).value);
console.log(xgen.next().value);
function counter() {
let index = 0;
return () => {
index += 1;
return index;
};
}
const gen = counter();
console.log(gen());
console.log(gen());
console.log(gen());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.