/
flow01.js
55 lines (44 loc) · 1.15 KB
/
flow01.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 单线程异步执行(并发流程控制1)
var LongTimeOperation = function(taskID) {
var id = taskID;
this.go = function(callback) {
console.log('this is longTimeOperation #' + id);
var delay = parseInt((Math.random() * 10000000) % 5000);
setTimeout(function() {
console.log('the task #' + id + ' cost ' + delay + ' ms.');
callback();
}, delay);
}
}
function f2() {
console.log('this is f2, i am callback.\n');
}
for(var i=0; i<5; i++) {
var task = new LongTimeOperation(i);
task.go(f2);
end();
}
function end() {
console.log("this is end task");
}
// 输出:
// this is longTimeOperation #0
// this is end task
// this is longTimeOperation #1
// this is end task
// this is longTimeOperation #2
// this is end task
// this is longTimeOperation #3
// this is end task
// this is longTimeOperation #4
// this is end task
// the task #4 cost 1053 ms.
// this is f2, i am callback.
// the task #1 cost 1912 ms.
// this is f2, i am callback.
// the task #3 cost 2437 ms.
// this is f2, i am callback.
// the task #0 cost 3195 ms.
// this is f2, i am callback.
// the task #2 cost 4446 ms.
// this is f2, i am callback.