Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
Retry react-query based on result

I want the query to retry until the expected result is met. How do I pass a condition into the function to decide if the query succeeded or not?

export async function waitUntilQuerySucceeds(
  id: Id,
  queryKey: QueryKey,
  queryClient: QueryClient,
  retryInterval = 500,
  amountOfAttempts = 12,
) {
  let attempts = 0
  let results: [] | undefined = queryClient.getQueryData([queryKey, id])
  while (attempts < amountOfAttempts) {
    const result = results?.find(p => p.Id === id)
    if (result) return true

    attempts += 1
    await queryClient.refetchQueries([queryKey, id])
    await wait(retryInterval)
    results = queryClient.getQueryData([queryKey, id])
  }
  return false
}

function wait(timeout: number) {
  return new Promise(resolve => {
    setTimeout(resolve, timeout)
  })
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

export async function waitUntilQuerySucceeds(
  id: Id,
  queryKey: QueryKey,
  queryClient: QueryClient,
  retryInterval = 500,
  amountOfAttempts = 12,
) {
  let isDataFetched = false
  let attempts = 0
  let results: [] | undefined = queryClient.getQueryData([queryKey, id])
  while (attempts < amountOfAttempts && !isDataFetched) {
    const result = results?.find(p => p.Id === id)
    if (result) isDataFetched = true;

    attempts += 1
    await queryClient.refetchQueries([queryKey, id])
    await wait(retryInterval)
    results = queryClient.getQueryData([queryKey, id])
  }
  return false
}

function wait(timeout: number) {
  return new Promise(resolve => {
    setTimeout(resolve, timeout)
  })
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!