Set it as a dependency in you __resource.lua or fxmanifest.lua
-- client
client_scripts {
'@async/async.lua',
...
}
-- server
server_scripts {
'@async/async.lua',
...
}
local asyncPool = Async.CreatePool()
for i=1, 100, 1 do
asyncPool.add(function(cb)
SetTimeout(1000, function()
cb(math.random(1, 50000))
end)
end)
end
-- ## Parallel all tasks at the same time ## --
asyncPool.startParallelAsync(function(results)
-- Trigger when all tasks are done
end)
-- Not async, just waiting for results
local results = asyncPool.startParallel()
-- ## Parallel with Limit ## --
asyncPool.startParallelLimitAsync(2, function(results)
-- Trigger when all tasks are done
end)
-- Not async, just waiting for results
local results = asyncPool.startParallelLimit(2)
-- ## Series all tasks one by one ## --
asyncPool.startSeriesAsync(function(results)
-- Trigger when all tasks are done
end)
-- Not async, just waiting for results
local results = asyncPool.startSeries()
local tasks = {}
for i=1, 100, 1 do
local task = function(cb)
SetTimeout(1000, function()
local result = math.random(1, 50000)
cb(result)
end)
end
table.insert(tasks, task)
end
Async.parallel(tasks, function(results)
-- Trigger when all tasks are done
print(json.encode(results))
end)
Async.parallelLimit(tasks, 2, function(results)
-- Trigger when all tasks are done
print(json.encode(results))
end)
Async.series(tasks, function(results)
-- Trigger when all tasks are done
print(json.encode(results))
end)