-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncEach.js
62 lines (58 loc) · 1.47 KB
/
asyncEach.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
56
57
58
59
60
61
62
/**
* each a object or array in async callback
* @include isNumber
* @param {array|number} list - array to loop
* @param {function} task - task({function} next, {object} value, {string|number} key)
* @param {function} [finishFn] - call this method when loop finished
* @param {string} [type] - set object type within [array|object], set by "isArrayLike" if not give this field
* @return {undefined}
*/
function asyncEach(list, task, cb, type){
var keys, type, key;
var i = -1;
if(typeof cb === 'string'){
type = cb;
cb = null;
}else{
type = type != null ? type : typeof o.length === 'number' ? 'array' : o.constructor.name;
}
if(list){
type = type != null ? type : typeof o.length === 'number' ? 'array' : o.constructor.name;
keys = type === 'object' ? Object.keys(list) : list;
next();
}
function next(err, rs){
if(err === false){
apply(cb, [null, rs]);
}else if(err != null){
apply(cb, [err, rs]);
}else{
switch(type.toLowerCase()){
case 'number':
}
setTimeout(function(){
i++;
if(err === false){
apply(cb, [null, rs]);
}else if(err){
apply(cb, [err, rs]);
}else{
if(type === 'for'){
if(i < list){
apply(task, [next, i, i, list]);
}else{
apply(cb, [null, rs]);
}
}else{
if(i < keys.length){
key = type === 'array' ? i : keys[i];
apply(task, [next, list[key], key, list]);
}else{
apply(cb, [null, rs]);
}
}
}
}, 0);
}
}
}