Skip to content

Instantly share code, notes, and snippets.

@susisu
Created February 29, 2016 17:18
Show Gist options
  • Select an option

  • Save susisu/8e38316b0f75d3269c75 to your computer and use it in GitHub Desktop.

Select an option

Save susisu/8e38316b0f75d3269c75 to your computer and use it in GitHub Desktop.
"use strict";
function ng(gf) {
let gen = gf();
let it = gen.next();
while (!it.done) {
if (it.value === null || it.value === undefined) {
return it.value;
}
it = gen.next(it.value);
}
return it.value;
}
function div(x, y) {
if (y === 0) {
return undefined;
}
return x / y;
}
let res1 = ng(function * () {
let x = 1, y = 2;
let z = (yield div(x, y)) * 2;
return z;
});
console.log(res1); // 1
let res2 = ng(function * () {
let x = 1, y = 0;
let z = (yield div(x, y)) * 2;
return z;
});
console.log(res2); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment