From 8c9300cb11f7542e8ebee82f81ab16e5a4284e2b Mon Sep 17 00:00:00 2001 From: userMacieG Date: Mon, 20 Jul 2020 16:27:01 +0200 Subject: [PATCH] Update async.lua (#3) --- async.lua | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/async.lua b/async.lua index b197f8f2..ba55f552 100644 --- a/async.lua +++ b/async.lua @@ -5,21 +5,17 @@ end Async = {} function Async.parallel(tasks, cb) - if #tasks == 0 then cb({}) return end local remaining = #tasks - local results = {} + local results = {} - for i=1, #tasks, 1 do - + for i = 1, #tasks, 1 do CreateThread(function() - tasks[i](function(result) - table.insert(results, result) remaining = remaining - 1; @@ -27,64 +23,51 @@ function Async.parallel(tasks, cb) if remaining == 0 then cb(results) end - end) - end) - end - end function Async.parallelLimit(tasks, limit, cb) - if #tasks == 0 then cb({}) return end local remaining = #tasks - local running = 0 - local queue = {} - local results = {} + local running = 0 + local queue, results = {}, {} for i=1, #tasks, 1 do table.insert(queue, tasks[i]) end local function processQueue() - if #queue == 0 then return end while running < limit and #queue > 0 do - local task = table.remove(queue, 1) running = running + 1 task(function(result) - table.insert(results, result) remaining = remaining - 1; - running = running - 1 + running = running - 1 if remaining == 0 then cb(results) end - end) - end CreateThread(processQueue) - end processQueue() - end function Async.series(tasks, cb)