Skip to content

Latest commit

 

History

History
20 lines (18 loc) · 400 Bytes

retry-fetch.md

File metadata and controls

20 lines (18 loc) · 400 Bytes

可以重试的请求

:::tip
一行代码搞定 :::

/**
 * @description 可重试的请求
 * @param {string} url 请求地址
 * @param {number} retry 重试次数
 */
export async function retryRequest(url: string, retry = 3): Promise<Response> {
  try {
    return await fetch(url)
  } catch (err) {
    return retry <= 0 ? Promise.reject(err) : retryRequest(url, retry - 1)
  }
}