Last active
November 24, 2016 09:02
-
-
Save JacksonTian/5084165 to your computer and use it in GitHub Desktop.
循环异步调用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var After = function (callback) { | |
this.index = 0; | |
this.counter = 0; | |
this.results = []; | |
this.callback = callback; | |
}; | |
After.prototype.group = function (callback) { | |
var that = this; | |
var index = that.index; | |
that.index++; | |
return function (data) { | |
that.counter++; | |
that.results[index] = callback ? allback(data) : data; | |
if (that.counter >= that.index) { | |
that.callback(that.results); | |
} | |
}; | |
}; |
Author
JacksonTian
commented
Mar 4, 2013
所以原有代码只要稍作改动就可以了:)
function foo (x, callback) {
var after = new After(callback);
for (i = x; i > 0; i--) {
var obj = new RemoteObj();
obj.rpc_call(x, after.group(function (result) {
// 把result的结果和x的值做计算
// ...
// ...
return result;
}));
}
}
14行有笔误
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment