-
-
Save zs-sz/16dd02f5da435dc4f639ac7b9f02d2c0 to your computer and use it in GitHub Desktop.
Modifying "sync" to do parallel stuff
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 sync(gen) { | |
| var iterable, resume, check, vals, ops; | |
| vals = []; | |
| ops = 0; | |
| check = function() { | |
| if (vals.length == ops) { | |
| if (ops == 1) { | |
| iterable.next(vals[0]); | |
| } | |
| else { | |
| iterable.next(vals); // resume with an array | |
| } | |
| } | |
| } | |
| resume = function(err, retVal) { | |
| var slot = ops; | |
| ops++; | |
| return function(err, retVal) { | |
| if (err) { | |
| iterable.raise(err); | |
| } | |
| else { | |
| vals[slot] = retVal; | |
| check(); | |
| } | |
| }; | |
| }; | |
| iterable = gen(resume); | |
| iterable.next(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment