短小强悍的 Javascript 异步调用库

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
var queue = function(funcs, scope) {
(function next(){
if(funcs.length > 0){
funcs.shift().apply(scope, [next].concat(Array.prototype.slice.call(arguments, 0)));
}
})();
}
// Example
var obj = { value: null };
queue([
function(callback){
var me = this;
setTimeout(function(){
me.value = 10;
callback(20);
});
},
function(callback, add){
console.log(this.value + add);
callback();
},
function(){
console.log(obj.value);
}
], obj);
// console: 30
// console: 10