Created
February 29, 2016 17:18
-
-
Save susisu/8e38316b0f75d3269c75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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