Harden Async's parallel against one call returning several times
This commit is contained in:
parent
75f02b2c0b
commit
52a7e883fa
1 changed files with 8 additions and 4 deletions
12
async.js
12
async.js
|
@ -39,13 +39,17 @@ function Async() {
|
||||||
var threads = arguments;
|
var threads = arguments;
|
||||||
var pending = threads.length;
|
var pending = threads.length;
|
||||||
var results = [];
|
var results = [];
|
||||||
|
var returned = [];
|
||||||
return function(f) {
|
return function(f) {
|
||||||
var useResult = function(i) {
|
var useResult = function(i) {
|
||||||
return function(x) {
|
return function(x) {
|
||||||
results[i] = x;
|
if(!returned[i]) {
|
||||||
pending--;
|
results[i] = x;
|
||||||
if(pending < 1) {
|
returned[i] = true;
|
||||||
f(results);
|
pending--;
|
||||||
|
if(pending < 1) {
|
||||||
|
f(results);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue