forked from thalesmello/exploring-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_coroutines_example.js
34 lines (30 loc) · 944 Bytes
/
06_coroutines_example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var Promise = require("bluebird"),
delay = Promise.delay,
promisify = Promise.promisify,
coroutine = Promise.coroutine,
aBootTime = 1000,
bBootTime = 1000,
promiseB;
coroutine(serverA)();
promiseB = coroutine(serverB)();
function* serverA() {
console.log("A: Booting up system...");
yield delay(aBootTime);
console.log("A: Checking network connection");
yield delay(500);
console.log("A: Request complex computation");
var serverHandler = yield promiseB;
var value = yield serverHandler();
console.log("A: Computation returned " + value);
}
function* serverB() {
console.log("B: Booting up system...")
yield delay(bBootTime);
console.log("B: Server up and running");
return promisify(coroutine(serverHandler));
function* serverHandler(callback) {
console.log("B: Starting heavy computation");
yield delay(2000);
callback(null, 42);
}
}