请求失败重传封装

一个异步请求/操作 模拟代码如下

const request = (url) => new Promise((resolve, reject) => {
  setTimeout(() => {
    Math.random() > 0.8 ? resolve(url+' ok') : reject(url + ' fail')
  }, 1000)
})
 
request('https://jirengu.com')
 .then(data => console.log(data))
 .catch(err => console.log(err))

要求:实现一个函数retry ,能对原函数进行封装,达到如下效果:如果如果请求失败,会自动重新发起请求,最多尝试n次。

const request = (url) => new Promise((resolve, reject) => {
  setTimeout(() => {
    Math.random() > 0.8 ? resolve(url+' ok') : reject(url + ' fail')
  }, 1000)
})
 
function retry() {
 ...
}
 
let request4 = retry(request, 4)
request4('https://jirengu.com')
  .then(data => console.log(data))  //只要成功1次,进入该逻辑
  .catch(err => console.log(err))   //连续失败4次,进入该逻辑

实现

const retry = (fn, n=3) => (...args) => {
  function run() {
    return fn(...args).then(data => {
      return Promise.resolve(data)
    }).catch(error => {
      n--
      if(n <= 0) {
        return Promise.reject(error)
      } else {
        return run()
      }
    }) 
  }
 
  return run()
}

完整测试代码

let i = 0
const request = (url) => new Promise((resolve, reject) => {
  console.log(`第${++i}次执行`)
  setTimeout(() => {
    Math.random() > 0.8 ? resolve(url + ' ok') : reject(url + ' fail')
  }, 1000)
})
 
 
const retry = (fn, n=3) => (...args) => {
  function run() {
    return fn(...args).then(data => {
      return Promise.resolve(data)
    }).catch(error => {
      n--
      if(n <= 0) {
        return Promise.reject(error)
      } else {
        return run()
      }
    }) 
  }
 
  return run()
}
 
 
let request4 = retry(request, 4)
request4('https://jirengu.com')
  .then(data => console.log(data))
  .catch(error => console.log(error))