Skip to content

tw.promise

Side: Shared (Client + Server)

Wraps callback-based functions into synchronous calls using Citizen.Await.

Functions

tw.promise.new(callback, ...)

Converts a callback-based function into a synchronous one. A resolver function is injected as the last argument of the callback. The call blocks until the resolver is invoked.

ParameterTypeDescription
callbackfunction(..., resolver)Function to wrap. resolver(...) is appended as the last argument
...anyArguments passed to the callback (before the resolver)

Returns: The value(s) passed to the resolver.

Examples

Wrap an async native

lua
-- Wait for a model to load synchronously
local loaded = tw.promise.new(function(model, resolve)
    RequestModel(model)
    while not HasModelLoaded(model) do
        Wait(0)
    end
    resolve(true)
end, GetHashKey("a_m_m_rancher_01"))

print(loaded) -- true

Wrap a callback-based API

lua
-- Turn an async callback into sync
local result = tw.promise.new(function(url, resolve)
    PerformHttpRequest(url, function(code, data)
        resolve(code, data)
    end, "GET")
end, "https://api.example.com/data")

Notes

  • The resolver function is always injected as the last argument of the callback.
  • The calling thread blocks (Citizen.Await) until the resolver is called. Do not forget to call it, or the thread will hang indefinitely.
  • You can pass multiple values to the resolver; they are all returned by tw.promise.new.

Premium RedM Scripts — Multi-Framework