不醒的梦

文章 评论 浏览 28

不醒的梦 2022-05-04 13:16:18

npm有个 zhuangbility

JavaScript 专题之花式表示 26 个字母

不醒的梦 2022-05-03 10:49:42
function myPromiseAny(arr) {
    return new Promise((resolve, reject) => {
        let pendingCount = arr.length;
        const error = new Error("all promise were rejected");
        error.errors = new Array(arr.length);

        function resolveHandler(v) {
            pendingCount -= 1;
            resolve(v);
        }
        function rejecthandler(index) {
            return function (e) {
                pendingCount -= 1;
                error.errors[index] = e;
                if (pendingCount === 0) {
                    reject(error);
                }
            }
        }

        arr.forEach((arrItem, index) => {
            Promise.resolve(arrItem).then(resolveHandler).catch(rejecthandler(index));
        });
    });
}

第 180 题:Promise.any 的作用,如何自己实现 Promise.any?

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

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