Its relatively simple
You make a promise
local p = promise.new()
This is an unresolved promise, you can do two things with this promise, resolve it, or reject it
p:resolve()
p:reject()
You can provide these two functions values
local someRandomTable = {
someUsefulData = true,
}
p:resolve(someRandomTable)
p:reject(someRandomTable)
You have the function Citizen.Await() which lets you await the promise
-- Citizen.Await handles the co-routines and polls the await, if a the Await gets rejected it will throw an error.
local someData = Citizen.Await(p)
-- do something after promise is resolved
If you reject a promise it will error, you can do something like this to handle that
local success, data = pcall(function()
local someRandomTable = Citizen.Await(p)
return someRandomTable
end)
if not success then
print(("We failed with reason %s"):format(data))
return
end
-- we succeeded! we can use the random table from data
Or just let it error, you should only use rejects if something is out of wack anyways.
6 Likes