-
Notifications
You must be signed in to change notification settings - Fork 23
/
07_async_await_example.js
37 lines (32 loc) · 1 KB
/
07_async_await_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
35
36
37
// This uses experimental syntax.
// To run it, use `regenerator -r 07_async_await_example.js | node`
var Promise = require("bluebird"),
delay = Promise.delay,
promisify = Promise.promisify,
coroutine = Promise.coroutine,
aBootTime = 1000,
bBootTime = 1000,
promiseB;
serverA();
promiseB = serverB();
async function serverA() {
console.log("A: Booting up system...");
await delay(aBootTime);
console.log("A: Checking network connection");
await delay(500);
console.log("A: Request complex computation");
var serverHandler = await promiseB;
var value = await serverHandler();
console.log("A: Computation returned " + value);
}
async function serverB() {
console.log("B: Booting up system...")
await delay(bBootTime);
console.log("B: Server up and running");
return promisify(serverHandler);
async function serverHandler(callback) {
console.log("B: Starting heavy computation");
await delay(2000);
callback(null, 42);
}
}