Wait until all promises complete even if some rejected

前端 未结 18 2284
醉酒成梦
醉酒成梦 2020-11-21 04:55

Let\'s say I have a set of Promises that are making network requests, of which one will fail:

// http://does-not-exist will throw a TypeError
va         


        
18条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 05:25

    This should be consistent with how Q does it:

    if(!Promise.allSettled) {
        Promise.allSettled = function (promises) {
            return Promise.all(promises.map(p => Promise.resolve(p).then(v => ({
                state: 'fulfilled',
                value: v,
            }), r => ({
                state: 'rejected',
                reason: r,
            }))));
        };
    }
    

提交回复
热议问题