From b50692fb595e0cfccbccea3daf42215cfcf57b5f Mon Sep 17 00:00:00 2001 From: Kaosiso Ezealigo Date: Wed, 13 Dec 2023 10:22:10 +0100 Subject: [PATCH] creates shortPoll func --- agenta-web/src/lib/helpers/utils.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/agenta-web/src/lib/helpers/utils.ts b/agenta-web/src/lib/helpers/utils.ts index 8cb87f5cb0..7b02123a95 100644 --- a/agenta-web/src/lib/helpers/utils.ts +++ b/agenta-web/src/lib/helpers/utils.ts @@ -299,3 +299,17 @@ export async function batchExecute( } return results } + +export const shortPoll = async ( + func: Function, + {delayMs, timeoutMs = 2000}: {delayMs: number; timeoutMs?: number}, +) => { + let startTime = Date.now() + let shouldContinue = true + while (shouldContinue && Date.now() - startTime < timeoutMs) { + try { + shouldContinue = await func() + } catch {} + await delay(delayMs) + } +}