Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function* gen() {
yeild 1;
return 2;
}
for (let value of gen()) {
print(value);
}
=>
function gen() {
let state = 0;
return {
next() {
switch (state) {
case 0:
state++;
return {value: 1, done: false};
case 1:
state++;
return {value: 2, done: true};
case 2:
return {value: undefined, done: true};
}
}
}
}
let $tmp = gen();
for (let {value, done} = $tmp.next(); !done; {value, done} = $tmp.next()) {
print(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment