Last active
August 7, 2025 21:18
-
-
Save lucivaldo/81ef623dd6ec5ce97c9a2f97586ce03c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* eslint-disable @typescript-eslint/no-explicit-any */ | |
| /** biome-ignore-all lint/suspicious/noExplicitAny: <> */ | |
| /** | |
| * Aguarda um tempo em milissegundos. | |
| * @param ms - Quantidade de milissegundos para aguardar. | |
| */ | |
| export function delay(ms: number): Promise<void> { | |
| return new Promise((resolve) => setTimeout(resolve, ms)); | |
| } | |
| /** | |
| * Aguarda um tempo aleatório entre `min` e `max` milissegundos. | |
| * @param min - Valor mínimo em milissegundos. | |
| * @param max - Valor máximo em milissegundos. | |
| */ | |
| export async function delayRandom(min: number, max: number): Promise<void> { | |
| const ms = Math.floor(Math.random() * (max - min + 1)) + min; | |
| await delay(ms); | |
| } | |
| /** | |
| * Envolve uma função assíncrona com um delay artificial antes de executá-la. | |
| * @param fn - Função assíncrona a ser executada com atraso. | |
| * @param ms - Delay em milissegundos antes de chamar a função. | |
| * @returns Uma nova função com atraso embutido. | |
| */ | |
| export function withDelay<T extends (...args: any[]) => Promise<any>>( | |
| fn: T, | |
| ms: number, | |
| ): T { | |
| return (async (...args: Parameters<T>): Promise<ReturnType<T>> => { | |
| await delay(ms); | |
| return fn(...args); | |
| }) as T; | |
| } | |
| /** | |
| * Envolve uma função assíncrona com delay aleatório antes de executá-la. | |
| * @param fn - Função assíncrona a ser executada. | |
| * @param min - Tempo mínimo do delay. | |
| * @param max - Tempo máximo do delay. | |
| * @returns Uma nova função com delay aleatório. | |
| */ | |
| export function withRandomDelay<T extends (...args: any[]) => Promise<any>>( | |
| fn: T, | |
| min: number, | |
| max: number, | |
| ): T { | |
| return (async (...args: Parameters<T>): Promise<ReturnType<T>> => { | |
| await delayRandom(min, max); | |
| return fn(...args); | |
| }) as T; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment