Fetch Retry

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

const fetchRetry = async (count: number, ...args: Parameters<typeof fetch>) => {
  const response = await fetch(...args)
  if (count > 2 || response.status !== 429) return response
  console.warn(`[429] Too Many Requests (Attempt ${count + 1})`)
  await sleep(500)
  return fetchRetry(count + 1, ...args)
}

Do you like my content?

Sponsor Me On Github