第 159 题:实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject

发布于 2022-09-12 18:52:56 字数 848 浏览 235 评论 42

实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject。

Promise.retry = (fun, limit = 5) => {
  return new Promise((resolve, reject) => {
    let __num = 1;
    let __run = () => {
      fun()
      .then(res => {
        resolve(res);
      })
      .catch(err => {
        if (__num++ >= limit) {
          reject(err)
        } else {
          console.log('retry again!!')
          __run()
        }
      })
    }
    __run()
  })
}
 
let k = 0
function test () {
  return new Promise((resolve, reject) => {
    if (++k >= 3) {
      resolve(k)
    } else {
      reject('hhh')
    }
  })
}
 
Promise.retry(test).then(res => {
  console.log('resolve: ', res)
}).catch(err => {
  console.log('reject: ', err)
})
// retry again!!
// retry again!!
// resolve: 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(42

百变从容 2022-05-04 13:54:13
Promise.retry = function (getPromise, num) {
  if (num === 1) return getPromise()
  return getPromise().then(res => res, () => Promise.retry(getPromise, --num))
}
夜空下最亮的亮点 2022-05-04 13:54:13
Promise.retry = function(fn,max = 3){
	let count = 0;
	return new Promise((resolve,reject)=>{
		 function handler(){
			fn().then((res)=>{
				resolve(res)
			}).catch((err)=>{
				count++;
				if(count >= max){
					reject(err)
				}else{
					handler()
				}
			})
		}

		handler()
	})
}
灼痛 2022-05-04 13:54:13
Promise.retry = (times = 10, fn) =>
  // eslint-disable-next-line no-async-promise-executor
  new Promise(async (resolve, reject) => {
    while (times > 0) {
      // eslint-disable-next-line no-param-reassign
      times -= 1;
      try {
        // eslint-disable-next-line no-await-in-loop
        const ret = await fn();
        return resolve(ret);
      } catch (err) {
        if (times <= 0) {
          return reject(err);
        }
      }
    }
  });

// Test case
const test = () => new Promise((resolve, reject) => {
  if (Math.random() < 0.2) {
    resolve('good luck');
  } else {
    reject(new Error('bad luck'));
  }
});


Promise.retry(2, test).then((ret) => {
  console.log(ret);
}).catch((err) => {
  console.log(err);
});
小鸟爱天空丶 2022-05-04 13:54:13
async function retry<T>(n: number, action: () => Promise<T>): Promise<T> {
  if (n <= 0) throw "Try all"
  try {
    let result = await action()
    return result
  } catch(_) {
    return retry(n - 1, action)
  }
}
妄想挽回 2022-05-04 13:54:13

比如重试 5 次

let time = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("error");
    }, 1000);
  });
};

async function f(retry = 5) {
  while (retry !== 0) {
    try {
      const result = await time();
      return result;
    } catch (e) {
      if (--retry === 0) {
        throw e
      }
    }
  }
}
悟红尘 2022-05-04 13:54:13
//函数
Promise.retry = function (promise, num) {
    console.log(num);
    return promise().then(res => res, function () {
        return num == 1 ? promise() : Promise.retry(promise, --num)
    });
}
//箭头函数
Promise.retry = (promise, num) =>
    promise().then(res => res, () =>
        num == 1 ? promise() : Promise.retry(promise, --num)
    );


//test
Promise.retry(Promise.resolve.bind(Promise, "reslove"), 5);
Promise.retry(Promise.reject.bind(Promise, "reject"), 5);
穿透光 2022-05-04 13:54:13

实现

对于常见的应用场景,至少应该满足重试次数和每次重试的间隔可配置,失败后立即重试意义并不大。

/**
 * Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 rejec
 * @param {(resolve, reject) => any} fn 执行函数
 * @param {number} max 最大尝试次数
 * @param {number} interval 尝试间隔 
 * @param {number} count 函数内部统计 
 */
export function retry(fn, max = 1, interval = 1000, count = 0) {
  --max
  ++count
  return new Promise((resolve, reject) => {
    const wrapResolve = (res) => {
      resolve(res)
    }
    const wrapReject = (error) => {
      if (max >= 0) {
        setTimeout(() => {
          console.log(`第${count}次重试`)
          resolve(retry(fn, max, interval, count))
        }, interval)
      } else {
        reject(error)
      }
    }
    fn(wrapResolve, wrapReject)
  })
}

Promise.retry = retry

测试

// 单纯的失败场景
function testFail(_, reject) {
  reject(new Error('error'))
}
Promise.retry(testFail, 10)
  .catch(error => conso.log(error)
// 单纯的成功场景
function testSuccess(resolve) {
  resolve('success')
}
Promise.retry(testSuccess, 10)
  .then(res => console.log(res))
// 重试成功的场景
let count = 0
function testRetrySuccess(resolve, reject) {
  ++count
  count > 3 ? resolve('success') : reject(new Error('error'))
}
Promise.retry(testRetrySuccess, 10)
  .then(res => console.log(res))
败给现实 2022-05-04 13:54:13
Promise.retry = function(fn,time=5){
  let flag = false
  return new Promise(async (resolve,reject)=>{
     while(time>0 && !flag){
	try{
	    const res = await fn()
	    resolve(res)
	   flag = true
	  }catch(e){
	      if(time<=0){
		 reject(e)
	      }
	    time--
	  }
       }
   })
}
不乱于心 2022-05-04 13:54:13

实现

含最大次数 & 延迟执行

Promise.retry = (fn, options = {}) => {
  const { max = 3, delay = 0 } = options
  let curMax = max
  const delayExec = () => delay && new Promise(resolve => setTimeout(resolve, delay))

  return new Promise(async (resolve, reject) => {
    do {
      try {
        const res = await fn()
        resolve(res)
        return
      } catch (error) {
        await delayExec()
        curMax--
        console.warn(`剩余次数${curMax}`)
        if (!curMax) reject(error)
      }
    } while (curMax > 0)
  })
}

测试

const resolveData = () => {
  return new Promise((resolve, reject) => {
    setTimeout(
      () => (Math.random() > 0.5 ? resolve('成功') : reject(new Error('失败')))
      , 1000,
    )
  })
}

;(async () => {
  try {
    const res = await Promise.retry(resolveData, { delay: 1000 })
    console.warn('result', res)
  } catch (error) {
    console.warn(error)
  }
})()
街角迷惘 2022-05-04 13:54:13
Promise.retry = function(fun,max=2){
      return fun().then(Promise.resolve(),(err)=>{
        if(!max){
          return Promise.reject(err);
        }else{
          return Promise.retry(fun,--max);
        }
      })
    }
    function getProm() {
      const n = Math.random();
      return new Promise((resolve, reject) => {
          setTimeout(() =>  n > 0.5 ? resolve(n) : reject(n));
      }, 1000);
    }

    Promise.retry(getProm).then((a)=>{
      console.log(a);
    });
哭了丶谁疼 2022-05-04 13:54:13
Promise.retry=function(fn,number=3){
    return function(...args){
        let time=0;
        var next=function(resolve,reject){
            fn(...args)
            .then(function(...res){
                console.log(res)
                resolve(...res)
            })
            .catch(function(...error){
                if(time===number){
                    reject(...error);
                    time=0;
                }else{
                    time++;
                    next(resolve,reject)
                }
            })
        }
        return new Promise(function(resolve,reject){
           next(resolve,reject)
        })
    }
}


function testSuccess(params) {
    return new Promise(function(resolve,reject) {
        resolve(params)   
    })
}
function tetestError(params) {
    return new Promise(function(resolve,reject) {
        reject(params)   
    })
}

let success=Promise.retry(testSuccess);
success('我是成功')
.then(function(params) {
    console.log(params)
})
.catch(function (err) {
    console.log(err)
})

let error=Promise.retry(tetestError);
error('我是成功')
.then(function(params) {
    console.log(params)
})
.catch(function (err) {
    console.log(err)
})

没有心的人〃 2022-05-04 13:54:13
Promise.prototype.retry = function(fn) {
  var resolve;
  var reject;
  
  var p = new Promise(function(res, rej) {
    resolve = res;
    reject = rej;
  });

  var retryReject = function(err) {
    fn(resolve, reject)
  }

  fn(resolve, retryReject);
  return p;
}

Promise.prototype.retry((resolve, reject) => {
  console.log('尝试')
  reject('err')
}).then(res => {console.log(res)}, rej => {
  console.log(rej);
})
顾冷 2022-05-04 13:54:13
Promise.retry = function(fn, times) {
  let n = 0
  return new Promise((resolve, reject) => {
    next(resolve, reject)
  })
  function next(resolve, reject) {
    fn().then(resolve).catch(e => {
      n++
      n === times ? reject(e) : next(resolve, reject)
    })
  }
}
溺ぐ爱和你が 2022-05-04 13:54:13

上面大部分有有个问题,不一定try-catch报错了才reject,得到不符合预期的结果也是reject的

永言不败 2022-05-04 13:54:13
Promise.retry = (promise, times) => {
  return new Promise((resolve, reject) => {
    const fn = async (promise, num = 0) => {
      try {
        const res = await promise;
        resolve(res);
      } catch (error) {
        if (num >= times) {
          reject(error);
        } else {
          fn(promise, num + 1);
        }
      }
    };
    fn(promise);
  });
};
要走干脆点丶 2022-05-04 13:54:13
Promise.retry = async (fn, times) => {
  while (true) {
    try {
      return await fn()
    } catch (e) {
      if (times-- === 0) {
        throw e
      }
    }
  }
}
农村范ル 2022-05-04 13:54:13
// async/await
Promise.retry = (asyncFn, times) => {
    // 把重试函数包装成 Promise
  let retPromsie = () => Promise.resolve().then(() => {
    try {
      return asyncFn()
    } catch(e) {
      throw Error(e)
    }
  })

  return new Promise((resolve, reject) => {
    let i=times
    const retry = async () => {
      if(i==0) return
      
      try {
        let ret = await retPromsie()
        ret!=undefined && resolve(ret)  
      } catch (e) {
        i==1 && reject(e)
      }

      i--
      retry()
    }
    retry()
  })
}
// const fn = () => a+b // ReferenceError: a is not defined
const fn = (a, b) => a+b // 3
Promise.retry(fn.bind(null, 1,2), 3).then(console.log, console.log)

// Promise
Promise.retry = (asyncFn, times) => {
  // 把重试函数包装成 Promise
  let retPromsie = () => Promise.resolve().then(() => {
    try {
      return asyncFn()
    } catch(e) {
      throw Error(e)
    }
  })
  const promiseFn = asyncFn instanceof Promise ? asyncFn : retPromsie

  if(times==1) return Promise.reject(promiseFn())
  
  return Promise.resolve(promiseFn()).then(null, () => Promise.retry(promiseFn, times-1))
}

// const fn = () => a+b // ReferenceError: a is not defined
const fn = (a, b) => a+b // 3
Promise.retry(fn.bind(null, 1,2), 3).then(console.log, console.log)
泪冰清 2022-05-04 13:54:13
Promise.retry = function (fn, max = 3) {
  let p = new Promise(fn);
  while (max) {
    p = p.catch(() => {
      console.log("失败");
      return new Promise(fn);
    });
    max -= 1;
  }
  return p;
};

// 测试  执行一个递增的i;i>1时会成功
let i = 0;
Promise.retry((resolve, reject) => {
  setTimeout(() => {
    if (i > 1) resolve("我成功了");
    i += 1;
    reject();
  }, 1000);
}).then(
  (res) => {
    console.log("res", res);
  },
  (err) => {
    console.log("err", err);
  }
);
酒绊 2022-05-04 13:54:13
var num = 0

var fn = () => {
    num++
    return new Promise((resolve, reject) => {
        if (num == 5) { // 修改这个数字可以模拟失败多少次
            resolve('成功')
        } else {
            reject('失败')
        }
    })
}

Promise.retry = function (promiseFn, times, interval) {
    return new Promise((resolve, reject) => {
        let recur = () => {
            promiseFn().then((data) => {
                resolve(data)
            }).catch(e => {
                times--
                console.log('失败了一次');
                if (times === 0) {
                    reject(e)
                } else {
                    setTimeout(() => {
                        recur()
                    }, interval);
                }
            })
        }
        recur()
    })
}

Promise.retry(fn, 3, 2000).then((data) => {
    console.log(data);
},(e) => {
    console.log(e);
})
莫多说 2022-05-04 13:54:13
function retry (fn, count = 10) {
  return new Promise(async (resolve, reject) => {
    while (count) {
      try {
        let res = await fn()
        resolve(res)
        return
      } catch (e) {
        if (!count) reject(e)
        count--
      }
    }
  })
}

应该先count--,不然永远也进不了catch那步啊

(() => {
    Promise._retry = function (fn, count) {
        return new Promise(async (resolve, reject) => {
            while (count) {
                try {
                    const res = await fn()
                    resolve(res)
                    return
                } catch (err) {
                    count--;
                    if(count) console.log('重试中,剩余次数:' + count); // 用例
                    if (!count) reject(err)
                }
            }
        });
    }

    // 用例
    let p = function () {
        return new Promise((resolve, reject) => {
            reject('没有了,没有总冠军了o(╯□╰)o');
        });
    };
    Promise._retry(p,10).then(res=>console.log(res)).catch(e=>console.log(e))
})();
岁月流歌 2022-05-04 13:54:13

弱弱的发一个,
思路:
fn 属于 Promise
while 需要 await 阻塞
递归 可以直接回调 异步

Promise.retry = function (fn, count) {
  return new Promise((resolve, reject) => {
    console.log(count)
    const pro = fn();
    if (!(pro instanceof Promise)) {
      i = 0;
      throw new Error('不是 Promise');
    }
    pro.then((res) => {
      resolve(res)
    }).catch(e => {
      if (count <= 0) {
        return reject(e);
      }
      Promise.retry(fn, count--);
    })
  })
}
无声静候 2022-05-04 13:54:13

promise里用try cath的你们不觉得诡异的么

(り。薆情海 2022-05-04 13:54:12
// 实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject 
Promise.retry = function(fn, num){
  return new Promise(async function(resolve, reject){
     while(num>0){
         try{
            const res = await fn();
            console.log('res: ', res);
            resolve(res)
            num = 0
          } catch(e){
            console.log('reject....');
            if(num === 1) reject(e);
            num--
          }
      }
  })
}

const test = () => {
  return new Promise((resolve, reject) => {
    const num = Math.floor(Math.random() * 10);
    if (num > 5) {
      resolve(num);
    } else {
      reject(`num 小于等于8`);
    }
  
  })
}

Promise
.retry(test, 5)
.then(res => {
  console.log('result: ', res);
})
.catch(err => {
  console.log('err: ', err);
})
╮执着的年纪 2022-05-04 13:54:12

更加具有抽象形式的retry,尝试反复执行一个方法,方法内部可以是异步请求,或者其他需要retry的动作,如请求动画等等。所以,我给出的retry函数的ts形式是这样的。

function retry(times:number,fn:Function){
    let counter = 0,maxTimes = times +1;
    let resp , err;
    while(counter < maxTimes){
           err = undefined;
           try{
                resp = fn();
           }catch(error){
                err = error;
           }
           if(err === undefined){
                 break;
           }
    }
    if(err){
       throw err;
    }

    return resp;
}

retry(3,()=>{
  console.log("test");
  throw new Error("retry error")
  return {}
})
飘落散花 2022-05-04 13:54:12
// 实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject
// 这道题刚开始做的时候,写漏了一个条件:超过一定次数才是真正的reject。
// 这里用for + break 去做次数限制
Promise.retry = function(fn, max = 5) {
    return new Promise(async (resolve, reject) => {
        let res = null
        for (let i in [...Array(max)]) {
            try {
               console.log('try: ', i)
               res = await fn()
               break
            } catch(e) {
                if(+i === (max - 1)) {
                    reject(e)
                    return
                }
            }
        }
        resolve(res)
    })
}

function fn () {
    let n = Math.random()
    return new Promise((resolve, reject) => {
        setTimeout(() => {
           console.log('Current random value: ', n)
           n > 0.75 ? resolve(n) : reject(n)
        }, 2000)
    })
}

Promise.retry(fn, 10)
.then(res => console.log('Resolve: ', res))
.catch(res => console.log('Reject: ', res))
高速公鹿^ε^ 2022-05-04 13:54:12

感觉题目可以清晰一些,指定retry的入参是啥

 Promise.retry = function (fn, times) {
   return new Promise((rs, rj) => {
      return fn().then((res) => {
        rs(res)
      }).catch((err) => {
        if (--times >= 0) {
          return Promise.retry(fn, times)
        } else {
          rj(err)
        }
      })
   })
 }
可可 2022-05-04 13:54:12

为啥感觉你们写的怎么这么复杂 。。。

Promise.retry = function (promiseFunc, num = 2) {
  return promiseFunc().then(null, (e) => {
    if (num > 0) {
      num -= 1;
      console.log("重试");
      return Promise.retry(promiseFunc, num);
    }
    return Promise.reject(e);
  });
};

再简化一下

Promise.retry = function (promiseFunc, num = 2) {
  return promiseFunc().then(null, (e) => num > 0 ? Promise.retry(promiseFunc, num - 1) : Promise.reject(e));
};
还不是爱你 2022-05-04 13:54:12

感觉题目可以清晰一些,指定retry的入参是啥

 Promise.retry = function (fn, times) {
   return new Promise((rs, rj) => {
      return fn().then((res) => {
        rs(res)
      }).catch((err) => {
        if (--times >= 0) {
          return Promise.retry(fn, times)
        } else {
          rj(err)
        }
      })
   })
 }

我就想问这一句:return fn().then((res) => rs(res));
题目没说fn函数返回的一定是Promise吧?普通函数调用then不就报错了

小姐丶请自重 2022-05-04 13:54:12
(function () {

  function retry(func, max = 2, err) {
    return new Promise((resolve, reject) => {
      return func()
        .then(resolve)
        .catch(err => {
          console.log('retry', max);
          if (max <= 0) {
            return reject(err)
          }
          return retry(func, max - 1);
        })
        .catch(reject);
      
    })
  }


  retry(() => Promise.resolve(1))
    .then(res => {
      console.log('成功', 1)
    })
    .catch(err => {
      console.log('fail');
    })

  retry(() => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        reject(111);
      }, 3000);
    })
  })
    .then(res => {
      console.log('成功', res)
    })
    .catch(err => {
      console.log('fail', err);
    })



})();
说不完的你爱 2022-05-04 13:54:12
if (Promise) {
  Promise.retry = function (fn, time) {
    return new Promise((resolve, reject) => {
      const retry = (i) => {
        return Promise.resolve(fn()).then(res => {
          resolve(res);
        }).catch(err => {
          if (i <= time) {
            return retry(++i);
          } else {
            reject(err);
          }
        })
      }
      return retry(0);
    });
  }
}

Promise.retry(function () {
 return new Promise((resolve, reject) => {
  setTimeout(() => {
    // resolve('resolve...');
    reject('reject...');
  }, 300);
 });
}, 3).then(res => {
  console.log('res: ', res);
}).catch(err => {
  console.log('err:', err);
});
允世 2022-05-04 13:54:12
Promise.retry = function (task, retryTimes) {
        if(retryTimes <= 0) {
          new Error('wrong params that retry times')
        }
        return new Promise((resolve, reject) => {
          function runTask() {
            if(retryTimes === 0) {
              reject(new Error('have no retry times'))
              return
            }
            task()
              .then((v) => {
                resolve(`success ${v}`);
              })
              .catch(() => {
                retryTimes--
                runTask()
              });
          }
          runTask();
        });
      };
木格〃 2022-05-04 13:54:02

方法一:在 catch() 中再次执行函数

Promise.retry = function (fn, times) {
  fn().then(console.log)
    .catch(e => {
      if (times > 0) {
        console.log('try again...')
        Promise.retry(fn, times - 1)
      } else {
        console.log('Error: No more times, now rejected.')
      }
    })
}

方法二:async 函数,结合 while 循环和 try...catch

Promise.retry = async function (fn, times) {
  while (times > 0) {
    try {
      const res = await fn()
      console.log(res)
      return
    } catch(e) {
      console.log('try again...')
      times--
    }
  }
  console.log('Error: No more times, now rejected.')
}

测试:

const test = function () {
  return new Promise((resolve, reject) => {
    const num = Math.floor(Math.random() * 10)
    if (num > 7) {
      resolve(num)
    } else {
      reject(new Error(num))
    }
  })
}

Promise.retry(test, 5)
她比我温柔 2022-05-04 13:54:02

记得几年前面试一次面试时问过类似的问题,当时绞尽脑汁也没想出解决办法



/**
 * promiseFn 返回promise 对象的函数
 * max       最大重试次数
 * interval  重试间隔时间
*/
Promise.retry = function (promiseFn, max=1, interval = 1000) {
    function excuteFn(max) {
        return promiseFn().then(res => {
            return Promise.resolve(res);
        }, err => {
            if (max <= 1) {
                return Promise.reject(err);
            } else {
                return new Promise((resolve, reject) => {
                    setTimeout(() => {
                        resolve(excuteFn(--max));
                    }, interval);
                })
            }
        })
    }
    return excuteFn(max)
}


// 测试用例
Promise.retry(ajax, 5, 1000)
    .then(res => console.log('retry-res->', res))
    .catch(err => console.log('retry-err->', err))



function ajax() {
    const n = Math.random();
    console.log('--->', n)
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (n > 0.9) {
                resolve(`成功返回值--->${n}`)
            } else {
                reject(`失败返回值--->${n}`)
            }
        }, 3000);
    })
}

小兔几 2022-05-04 13:51:11
/** 
 * 实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject 
*/
Promise.retry = function(fn, limit) {
  let i = 0
  return new Promise((resolve, reject) => {
    function retry() {
      console.log('try!', i)
      fn().then(res => {
        resolve()
      }).catch(err => {
        i++
        if(i > limit - 1) {
          reject(err)
        } else {
          retry()
        }
      })
    }
    retry()
  })
  
}
Promise.retry(() => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      Math.random() > .5 ? resolve() : reject()
    }, 300);
  })
}, 5).then(res => {
  console.log('resolve')
}).catch(err => {
  console.log('reject')
})
恏ㄋ傷疤〃忘ㄋ疼 2022-05-04 13:47:49
function retry (fn, count = 10) {
  return new Promise(async (resolve, reject) => {
    while (count) {
      try {
        let res = await fn()
        resolve(res)
        return
      } catch (e) {
        if (!count) reject(e)
        count--
      }
    }
  })
}
左秋 2022-05-04 12:53:37
 Promise.retry = function(fn, num){
    return new Promise(function(resolve, reject){
       while(num>0){
           try{
                  const res = await fn
                  resolve(res)
                  num = 0
            } catch(e){
                  if(!num) reject(e)
            }
            num --
        }
    })
}
鲜肉鲜肉永远不皱 2022-05-04 12:28:30

写的不好,有好的写法大家可以指点一下,感谢

/*
          实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject
        */

      // 同步获得Promise状态和值,这样写很low,要是更好的写法大家call我一下
      async function checkPromise(getFlag) {
        try {
          let res = await getFlag();
          return { flag: true, value: res };
        } catch (e) {
          return { flag: false, value: e };
        }
      }

      async function retry(pro, count = 10) {
        let result = {
            flag: false,
            value: null,
          },
          length = count;
        while (count && !result.flag) {
          console.log(`调用了${length - count + 1}次`);
          result = await checkPromise(pro);
          const { flag, value } = result;
          if (!flag) {
            count--;
          }
        }
        return result.value;
      }
      Promise.retry = retry;

      getFlag = function () {
        return Math.random() > 0.5 ? Promise.resolve(1) : Promise.reject(-1);
      };

      Promise.retry(getFlag, 10)
        .then((res) => {
          console.log("成功结果", res);
        })
        .catch((e) => {
          console.log("失败结果", e);
        });
蛮可爱 2022-05-02 01:37:29
Promise.retry = function (promiseFn, times = 3) {
  return new Promise(async (resolve, reject) => {
    while (times--) {
      try {
        var ret = await promiseFn();
        resolve(ret);
        break;
      } catch (error) {
        if (!times) reject(error);
      }
    }
  });
};
function getProm() {
    const n = Math.random();
    return new Promise((resolve, reject) => {
        setTimeout(() =>  n > 0.9 ? resolve(n) : reject(n), 1000);
    });
}
Promise.retry(getProm);
~没有更多了~

关于作者

假扮的天使

暂无简介

0 文章
0 评论
24 人气
更多

推荐作者

醉城メ夜风

文章 0 评论 0

远昼

文章 0 评论 0

平生欢

文章 0 评论 0

微凉

文章 0 评论 0

Honwey

文章 0 评论 0

qq_ikhFfg

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文