Skip to content
GAMPEONCreate
New project
Preview: this works today, but details can still change.

Promises

Wait for a result that arrives later, for example from a callback.

promise.new(executor?) --> promise

Create 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(...) --> promise

Create 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(err) --> promise

Create 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(promises) --> promise

Wait 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(promises) --> promise

Resolve 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(promises) --> promise

Same 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(p) --> any

Pause 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.

Report a problem with this pageOther ways to give feedback