UNPKG

1.38 kBJavaScriptView Raw
1var Promise = require('bluebird');
2var async = require('..').async;
3var await = require('..').await;
4
5
6// A slow asynchronous function, written in async/await style.
7var longCalculation = async (function (seconds, result) {
8 await(Promise.delay(seconds * 1000));
9 return result;
10});
11
12// A pair of synchronous-looking compound operations, written in async/await style.
13var compoundOperationA = async (function () {
14 console.log('A: zero');
15 console.log(await(longCalculation(1, 'A: one')));
16 console.log(await(longCalculation(1, 'A: two')));
17 console.log(await(longCalculation(1, 'A: three')));
18 return 'A: Finished!';
19});
20var compoundOperationB = async (function () {
21 await(longCalculation(0.5, '')); // Fall half a second behind A.
22 console.log('B: zero');
23 console.log(await(longCalculation(1, 'B: one')));
24 console.log(await(longCalculation(1, 'B: two')));
25 console.log(await(longCalculation(1, 'B: three')));
26 return 'B: Finished!';
27});
28
29// Start both compound operations.
30compoundOperationA().then(function (result) { console.log(result); });
31compoundOperationB().then(function (result) { console.log(result); });
32
33// Outputs (with half second delays between lines):
34// A: zero
35// B: zero
36// A: one
37// B: one
38// A: two
39// B: two
40// A: three
41// A: Finished!
42// B: three
43// B: Finished!