Skip to content

Instantly share code, notes, and snippets.

@riston
Created October 23, 2015 19:38
Show Gist options
  • Save riston/0b0cee9b07219ec6044d to your computer and use it in GitHub Desktop.
Save riston/0b0cee9b07219ec6044d to your computer and use it in GitHub Desktop.
Generators execution flow example
"use strict";
const vo = require("vo");
function wait(fn, time) {
time = time || 0;
if (typeof fn === "function") {
fn = fn();
}
return new Promise(resolve => setTimeout(() => {
resolve(fn);
}, time));
}
// A and B are executed in series
function* A() {
console.log("A");
return yield ["A"];
}
function* B() {
console.log("B");
return yield ["B"];
}
// C and D are executed in parallel
function* C() {
const randTime = ~~(Math.random() * 3000);
return yield wait(() => {
console.log("C");
return ["C", "F"]
}, randTime);
// return yield ["C"];
}
function* D() {
console.log("D");
return yield ["D"];
}
vo(A, B, [C, D])(function (err) {
if (err) throw err;
console.log("Result", err, arguments);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment