Exercise 7 solution
How to request in parallel the files, but print them out sequentially with generators and a runner utility
ASQ()
.runner(function *loadFiles(){
// Request all files at once in
// "parallel" via `getFile(..)`.
var p1 = getFile("file1");
var p2 = getFile("file2");
var p3 = getFile("file3");
// Render as each one finishes,
// but only once previous rendering
// is done.
var text1 = yield p1;
output(text1);
var text1 = yield p2;
output(text2);
var text1 = yield p3;
output(text3);
output("Complete!");
})
Promises and thunks eliminate the time aspect (the most important point)This is really important. Code becomes so much cleaner and easier to understand.You can
yield promise.all
or whatever else you need. This how you should write your code from now on. No more callbacks.