悟红尘

文章 评论 浏览 772

悟红尘 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);

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

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

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