Harden async's parallel against one function returning multiple times

This commit is contained in:
Tissevert 2018-12-14 23:31:09 +01:00
parent 99d3a34790
commit bdf11a96ca

View file

@ -39,14 +39,18 @@ 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) {
if(!returned[i]) {
results[i] = x; results[i] = x;
returned[i] = true;
pending--; pending--;
if(pending < 1) { if(pending < 1) {
f(results); f(results);
} }
}
}; };
}; };
for(var i = 0; i < threads.length; i++) { for(var i = 0; i < threads.length; i++) {