重试修饰器实现 retry 基于Promise

  function retry(fn, times, delay) {
    let currTimes = 1;
    return new Promise(function(resolve, reject) {
        function attempt() {
            fn().then(resolve).catch(function(err) {
                console.log(`第 ${currTimes} 次尝试`);
                if(times === 0) {
                    reject(err)
                } else {
                    times--;
                    currTimes++;
                    setTimeout(attempt(), delay)
                }
            })
        }

        attempt()
    })
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
支付宝领个红包就是对我最大的赞赏了
shijf
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!