Promises
Wait for a result that arrives later, for example from a callback.
promise.new
Section titled “promise.new”promise.new(executor?) --> promiseCreate a promise that is resolved later. Server and client.
Call p:resolve(value) or p:reject(err) once. p:next(onResolved, onRejected) chains a follow-up and returns a new promise.
| Parameter | Type | Description |
|---|---|---|
executor (optional) |
fun(resolve: function, reject: function) |
Optional: called right away with resolve and reject. |
Returns
promise: The promise.
local p = promise.new()PerformHttpRequest(url, function(status, body) p:resolve(body)end)local body = Citizen.Await(p)Compared to FiveM: Same as in FiveM.
promise.resolve
Section titled “promise.resolve”promise.resolve(...) --> promiseCreate a promise that is already resolved. Server and client.
| Parameter | Type | Description |
|---|---|---|
... |
any |
Values. |
Returns
promise: The promise.
local p = promise.resolve(42)Compared to FiveM: Same as in FiveM.
promise.reject
Section titled “promise.reject”promise.reject(err) --> promiseCreate a promise that is already rejected. Server and client.
| Parameter | Type | Description |
|---|---|---|
err |
any |
Error. |
Returns
promise: The promise.
local p = promise.reject('no money')Compared to FiveM: Same as in FiveM.
promise.all
Section titled “promise.all”promise.all(promises) --> promiseWait for several promises; resolves with a list of their first values. Server and client.
| Parameter | Type | Description |
|---|---|---|
promises |
promise[] |
Promises. |
Returns
promise: Resolves when all are resolved, rejects on the first error.
local results = Citizen.Await(promise.all({ p1, p2 }))Compared to FiveM: Same as in FiveM.
promise.first
Section titled “promise.first”promise.first(promises) --> promiseResolve with the first promise that finishes. Server and client.
| Parameter | Type | Description |
|---|---|---|
promises |
promise[] |
Promises. |
Returns
promise: The new promise.
local winner = Citizen.Await(promise.first({ p1, p2 }))Compared to FiveM: Same as in FiveM.
promise.race
Section titled “promise.race”promise.race(promises) --> promiseSame as promise.first. Server and client.
| Parameter | Type | Description |
|---|---|---|
promises |
promise[] |
Promises. |
Returns
promise: The new promise.
local winner = Citizen.Await(promise.race({ p1, p2 }))Compared to FiveM: Same as in FiveM.
Citizen.Await
Section titled “Citizen.Await”Citizen.Await(p) --> anyPause the current thread until a promise is resolved, then return its values. Server and client.
| Parameter | Type | Description |
|---|---|---|
p |
promise |
The promise. |
Returns
any: Values from resolve. A rejected promise raises the error here.
local data = Citizen.Await(p)Compared to FiveM: Same as in FiveM. Only inside a thread, event handler or command.