# 封装重试函数

function retry(originFunc, time) {
  let count = 0;
  return new Promise((resolve, reject) => {
    const tryFunc = function() {
      originFunc().then(
        res => {
          console.log('resolve', res);
          resolve(res);
        },
        error => {
          if (count < time) {
            count++;
            tryFunc();
          } else {
            console.log('reject', error);
            reject(error);
          }
        }
      );
    };
    tryFunc();
  });
}

let originFunc = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('reject');
      reject('请重试!');
    }, 500);
  });
};
retry(originFunc, 5);