Created
February 20, 2014 04:23
-
-
Save getify/9107067 to your computer and use it in GitHub Desktop.
exploring generators + parallel-promises for a single "step"
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
// 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