ThymonA / redm-async

Async for RedM by TIGO

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RedM Async

RedM-async Async utilities for RedM

INSTALLATION

Set it as a dependency in you __resource.lua or fxmanifest.lua

-- client
client_scripts {
	'@async/async.lua',
	...
}

-- server
server_scripts {
	'@async/async.lua',
	...
}

USAGE NEW WAY

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()

USAGE OLD WAY

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)

About

Async for RedM by TIGO

License:MIT License


Languages

Language:Lua 100.0%