-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-test.js
50 lines (40 loc) · 1.07 KB
/
async-test.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
var asyncTree= require('./asyncTree');
function getLevelOne(callback){
var initialArgs = [0, 1, 2, 3, 4];
callback(null, initialArgs);
}
function getLevelTwoItem(callback){
var item = this.item;
const results = new Array(item||1).fill().map(x => 2*item);
setTimeout(function(){
callback(null, results);
}, [0, 2000, 2000, 3000, 1, 1][item]);
}
function getLevelThreeItem(callback){
var item = this.item;
console.log('---- ',this.results);
setTimeout(function(){
const results = new Array(item/2||1).fill().map(x => 2*item);
callback(null, results);
}, Math.floor(Math.random() * 3000));
}
var finalResultsArray = [];
function doneCallback(){
console.log(`finished with ${finalResultsArray.length} results`);
}
function eachCallback(err, result){
finalResultsArray.push(this.results);
console.log(result);
}
var functionArray = [
getLevelOne,
getLevelTwoItem,
getLevelThreeItem
];
asyncTree({
functionArray,
concurrency: 2,
delay: 100, //in ms
doneCallback,
eachCallback
});