@fivem-ts/shared
Version:
FiveM Typescript wrapper shared part
37 lines (36 loc) • 2.12 kB
TypeScript
/**
* 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.
*
*
* @example
* ```ts
* 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));
* ```
*
* @template T
*
* @param {Promise<T> | 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.
* @param [errorMessage='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'`.
* @param {number | boolean} [timeout=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.
*
* @returns {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.
*
* @throws {Error} 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.
*/
export declare function waitFor<T>(cb: () => Promise<T> | T, errorMessage?: string, timeout?: number | boolean): Promise<T>;