forked from yortus/asyncawait
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterleaved.js
More file actions
43 lines (38 loc) · 1.3 KB
/
interleaved.js
File metadata and controls
43 lines (38 loc) · 1.3 KB
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
38
39
40
41
42
43
var Promise = require('bluebird');
var async = require('..').async;
var await = require('..').await;
// A slow asynchronous function, written in async/await style.
var longCalculation = async (function (seconds, result) {
await(Promise.delay(seconds * 1000));
return result;
});
// A pair of synchronous-looking compound operations, written in async/await style.
var compoundOperationA = async (function () {
console.log('A: zero');
console.log(await(longCalculation(1, 'A: one')));
console.log(await(longCalculation(1, 'A: two')));
console.log(await(longCalculation(1, 'A: three')));
return 'A: Finished!';
});
var compoundOperationB = async (function () {
await(longCalculation(0.5, '')); // Fall half a second behind A.
console.log('B: zero');
console.log(await(longCalculation(1, 'B: one')));
console.log(await(longCalculation(1, 'B: two')));
console.log(await(longCalculation(1, 'B: three')));
return 'B: Finished!';
});
// Start both compound operations.
compoundOperationA().then(function (result) { console.log(result); });
compoundOperationB().then(function (result) { console.log(result); });
// Outputs (with half second delays between lines):
// A: zero
// B: zero
// A: one
// B: one
// A: two
// B: two
// A: three
// A: Finished!
// B: three
// B: Finished!