waitFor
Poll a callback every frame until it returns a non-nil value, with a timeout. A polling promise for FiveM.
Poll a callback every frame until it returns a non-nil value, with a timeout. A polling promise for FiveM.
lib.waitFor(cb, errorMessage?, timeout?) polls a callback every frame until it returns a non-nil
value, then returns that value. It's a polling-based promise for FiveM — wait for an entity to spawn, a
model to load, or an export to come online.
-- wait for the player to enter a vehicle
local vehicle = lib.waitFor(function()
local veh = GetVehiclePedIsIn(PlayerPedId(), false)
if veh ~= 0 then return veh end
end, 'player never entered a vehicle', 10000)cbfunctionrequiredEvaluated each frame. Return nil while the condition isn't met, and the awaited value once it is.
errorMessagestringoptionalMessage thrown on timeout. Default 'failed to resolved callback'.
timeoutnumber | falseoptionalMax wait in ms. Omitted = 1000. Passing false disables the timeout (infinite wait).
Returns the first non-nil value the callback produces. Throws via error() if the timeout is reached
first.
The default timeout is just 1000 ms — bump it for long operations (model load, DB query), and avoid
false (infinite wait) in production, since a condition that never resolves blocks the thread forever.
The callback runs ~60×/second, so keep it light — no heavy native or DB query each frame. One gotcha:
only nil triggers another tick, so a callback that returns false resolves immediately with
false. It uses Wait, so it must run inside a Citizen thread, and it throws on timeout — wrap it in
pcall to handle failure.