Global Functions
Some functions are automatically added when you use jo_libs in your script.
jo.async
A namespace that provides async/promise-based versions of any function, global or from jo.* modules.
All functions accessed through jo.async are automatically wrapped to run in a separate thread and return a promise.
jo.async > Features
- Lazy loading: Functions are wrapped only when accessed
- Multi-return support: Handles functions that return multiple values
- Chainable promises: Use
:next()to chain async operations - Parallel execution: Start multiple async operations and await them later
jo.async > Syntax
jo.async.moduleName.functionName(...)
jo.async.globalFunction(...)
jo.async > Examples
Basic usage with .next():
jo.async.screen.fadeOut():next(function()
print('Screen faded out!')
end)
Multi-return values:
-- If myFunction() returns multiple values: a, b, c
jo.async.myModule.myFunction():next(function(a, b, c)
print(a, b, c) -- All values are automatically unpacked
end)
Using with :await() (recommended):
local a, b, c = jo.async.myFunction():await()
Or with Citizen.Await:
local results = Citizen.Await(jo.async.myFunction())
local a, b, c = table.unpack(results)
Parallel execution:
-- Launch multiple async operations
local promise1 = jo.async.func1()
local promise2 = jo.async.func2()
local promise3 = jo.async.func3()
-- Do other work while they execute in parallel
-- Await results when needed
local result1 = promise1:await()
local result2 = promise2:await()
local result3 = promise3:await()
Error handling:
jo.async.myFunction():next(
function(result)
print('Success:', result)
end,
function(error)
print('Error:', error)
end
)
jo.ready()
A function fired when all modules are loaded
jo.ready() > Syntax
jo.ready(cb)
jo.ready() > Parameters
cb : function
The function to execute
jo.ready() > Example
jo.ready(function()
print('All modules are loaded')
end)
jo.stopped()
A function fired when the script is stopped
jo.stopped() > Syntax
jo.stopped(cb)
jo.stopped() > Parameters
cb : function
The function to execute
jo.stopped() > Example
jo.stopped(function()
print(GetCurrentResourceName() .. ' has been stopped')
end)
jo.onResourceStart()
A function fired when a resource is fully started
jo.onResourceStart() > Syntax
jo.onResourceStart(resourceName, cb)
jo.onResourceStart() > Parameters
resourceName : string
The name of the resource to listen for
cb : function
The function to execute
jo.onResourceStart() > Example
jo.onResourceStart('myResource', function()
print('myResource has been started')
end)