From cf61bbd0c394815358d3993da6a9e2240322c93c Mon Sep 17 00:00:00 2001 From: ASTROWwwW Date: Wed, 15 Jul 2026 16:23:01 +0200 Subject: [PATCH 1/2] feat(esx_lib): add xLib.waitFor --- [core]/esx_lib/imports/waitFor/shared.lua | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 [core]/esx_lib/imports/waitFor/shared.lua diff --git a/[core]/esx_lib/imports/waitFor/shared.lua b/[core]/esx_lib/imports/waitFor/shared.lua new file mode 100644 index 00000000..d680dc93 --- /dev/null +++ b/[core]/esx_lib/imports/waitFor/shared.lua @@ -0,0 +1,45 @@ +---Yields the current thread until the callback returns a non-nil value. +---@generic T +---@param cb fun(): T? +---@param errMessage? string +---@param timeout? number | false Error out after `~x` ms if the callback hasn't resolved. Defaults to 1000, unless set to `false`. +---@param interval? integer Polling interval in ms. Defaults to 0. A value above `timeout` overshoots it by one cycle. +---@return T +---@async +function xLib.waitFor(cb, errMessage, timeout, interval) + xLib.verify(cb, 'function', true) + + if interval then + xLib.verify(interval, 'int', true) + end + + local value = cb() + + if value ~= nil then + return value + end + + if timeout ~= false and type(timeout) ~= 'number' then + timeout = 1000 + end + + local start = timeout and GetGameTimer() + + while value == nil do + Wait(interval or 0) + + value = cb() + + if value == nil and timeout then + local elapsed = GetGameTimer() - start + + if elapsed > timeout then + return error(('%s (waited %.1fms)'):format(errMessage or 'failed to resolve callback', elapsed), 2) + end + end + end + + return value +end + +return xLib.waitFor From 40139a1a4a22b8ae0f7a6cd5077c032f4f639fb2 Mon Sep 17 00:00:00 2001 From: Astrxw <116487456+ASTROWwwW@users.noreply.github.com> Date: Wed, 15 Jul 2026 17:20:18 +0200 Subject: [PATCH 2/2] Update shared.lua --- [core]/esx_lib/imports/waitFor/shared.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/[core]/esx_lib/imports/waitFor/shared.lua b/[core]/esx_lib/imports/waitFor/shared.lua index d680dc93..c86ccb78 100644 --- a/[core]/esx_lib/imports/waitFor/shared.lua +++ b/[core]/esx_lib/imports/waitFor/shared.lua @@ -20,7 +20,7 @@ function xLib.waitFor(cb, errMessage, timeout, interval) end if timeout ~= false and type(timeout) ~= 'number' then - timeout = 1000 + timeout = 5000 end local start = timeout and GetGameTimer()