From bdf11a96caeb8c9991402c87b6acd3272afba177 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Fri, 14 Dec 2018 23:31:09 +0100 Subject: [PATCH] Harden async's parallel against one function returning multiple times --- src/async.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/async.js b/src/async.js index 53f6952..996771f 100644 --- a/src/async.js +++ b/src/async.js @@ -39,13 +39,17 @@ function Async() { var threads = arguments; var pending = threads.length; var results = []; + var returned = []; return function(f) { var useResult = function(i) { return function(x) { - results[i] = x; - pending--; - if(pending < 1) { - f(results); + if(!returned[i]) { + results[i] = x; + returned[i] = true; + pending--; + if(pending < 1) { + f(results); + } } }; };