Skip to content

Instantly share code, notes, and snippets.

@laser
Last active June 10, 2016 23:56
Show Gist options
  • Save laser/8285400 to your computer and use it in GitHub Desktop.
Save laser/8285400 to your computer and use it in GitHub Desktop.
Modifying "sync" to do parallel stuff
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