Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

ThymonA/redm-async

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)