diff --git a/frontend/src/lib/utils.tsx b/frontend/src/lib/utils.tsx index b37a126d607e8..302f81c2d0657 100644 --- a/frontend/src/lib/utils.tsx +++ b/frontend/src/lib/utils.tsx @@ -391,8 +391,16 @@ export function idToKey(array: Record[], keyField: string = 'id'): return object } -export function delay(ms: number): Promise { - return new Promise((resolve) => window.setTimeout(resolve, ms)) +export function delay(ms: number, signal?: AbortSignal): Promise { + return new Promise((resolve, reject) => { + const timeoutId = setTimeout(resolve, ms) + if (signal) { + signal.addEventListener('abort', () => { + clearTimeout(timeoutId) + reject(new DOMException('Aborted', 'AbortError')) + }) + } + }) } export function clearDOMTextSelection(): void { diff --git a/frontend/src/queries/query.ts b/frontend/src/queries/query.ts index 29d08863a62c0..ded22d89a9979 100644 --- a/frontend/src/queries/query.ts +++ b/frontend/src/queries/query.ts @@ -35,7 +35,7 @@ import { isTimeToSeeDataSessionsQuery, } from './utils' -const QUERY_ASYNC_MAX_INTERVAL_SECONDS = 10 +const QUERY_ASYNC_MAX_INTERVAL_SECONDS = 5 const QUERY_ASYNC_TOTAL_POLL_SECONDS = 300 //get export context for a given query @@ -115,15 +115,9 @@ async function executeQuery( let currentDelay = 300 // start low, because all queries will take at minimum this while (performance.now() - pollStart < QUERY_ASYNC_TOTAL_POLL_SECONDS * 1000) { - await delay(currentDelay) + await delay(currentDelay, methodOptions?.signal) currentDelay = Math.min(currentDelay * 2, QUERY_ASYNC_MAX_INTERVAL_SECONDS * 1000) - if (methodOptions?.signal?.aborted) { - const customAbortError = new Error('Query aborted') - customAbortError.name = 'AbortError' - throw customAbortError - } - const statusResponse = await api.queryStatus.get(response.id) if (statusResponse.complete || statusResponse.error) {