Skip to content

Instantly share code, notes, and snippets.

@getify
Created February 20, 2014 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/9107067 to your computer and use it in GitHub Desktop.
Save getify/9107067 to your computer and use it in GitHub Desktop.
exploring generators + parallel-promises for a single "step"
// is this how most people see CLEANLY doing the task of
// firing off multiple "parallel" tasks at a single step
// in a generator, using parallel promises (aka, `all()`)?
//
// or is there another pattern for "parallel" generator steps?
spawn(function*(){
var [x,y] = yield Promise.all(getValue(10),getValue(12));
var z = yield getValue(20);
console.log("Meaning of life: " + (x+y+z)); // Meaning of life: 42
});
function getValue(v) {
return new Promise(function(resolve,reject){
setTimeout(function(){ resolve(v); },100); // faking async with timer
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment