Created
October 23, 2015 19:38
-
-
Save riston/0b0cee9b07219ec6044d to your computer and use it in GitHub Desktop.
Generators execution flow example
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
"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