a library to speed up the development of Fivem scripts and frameworks
fivem-ts - Documentation v0.7.5 • Docs
fivem-ts - Documentation v0.7.5 / Shared / waitFor
waitFor<
T
>(cb
,errorMessage
,timeout
):Promise
<T
>
Waits for a callback function to return a defined value within a specified timeout.
This function repeatedly calls the provided callback until it returns a non-undefined
value or the specified timeout is reached. If the timeout is false
, the function will
wait indefinitely until the callback returns a non-undefined
value or the script is terminated.
• T
• cb
A callback function that returns a value of type T
or a Promise
of type T
.
The function will be called repeatedly until it returns a non-undefined
value.
• errorMessage: string
= 'Callback failed to resolve'
An optional custom error message to use if the timeout is reached before
the callback returns a value. Defaults to 'Callback failed to resolve'
.
• timeout: number
| boolean
= 1000
An optional timeout duration in milliseconds. If false
, it disables the
timeout, making the function wait indefinitely. If not provided or if null
,
defaults to 1000
milliseconds. If provided, must be a non-negative number.
Promise
<T
>
A promise that resolves with the non-undefined
value returned by the callback,
or rejects with an error if the timeout is reached before a value is returned.
Error if the timeout is reached before the callback returns a non-undefined
value.
The error message includes the provided or default error message and the elapsed
time in milliseconds.
async function exampleCallback(): Promise<number | undefined> {
return new Promise(resolve => setTimeout(() => resolve(Math.random() > 0.5 ? 42 : undefined), 500));
}
waitFor(exampleCallback, 'Value not returned in time', 2000)
.then(value => console.log('Received value:', value))
.catch(error => console.error('Error:', error));