Last active
March 7, 2017 18:36
-
-
Save creationix/5762837 to your computer and use it in GitHub Desktop.
universal callback/continuable/thunk generator runner
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
| function run(generator) { | |
| // Pass in resume for no-wrap function calls | |
| var iterator = generator(resume); | |
| var data = null, yielded = false; | |
| next(); | |
| check(); | |
| function next(item) { | |
| var cont = iterator.next(item).value; | |
| // Pass in resume to continuables if one was yielded. | |
| if (typeof cont === "function") cont(resume()); | |
| yielded = true; | |
| } | |
| function resume() { | |
| var done = false; | |
| return function () { | |
| if (done) return; | |
| done = true; | |
| data = arguments; | |
| check(); | |
| }; | |
| } | |
| function check() { | |
| while (data && yielded) { | |
| var err = data[0]; | |
| var item = data[1]; | |
| data = null; | |
| yielded = false; | |
| if (err) return iterator.throw(err); | |
| next(item); | |
| yielded = true; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, creationix.
May I ask you a question?
In the following code, you pass an argument to iterator.next() method.
But whenever I pass any value to iterator.next(), there's nothing to happen.
What's the meaning of passing an argument to iterator.next() ?