Wait until all promises complete even if some rejected

前端 未结 18 2524
醉酒成梦
醉酒成梦 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:41

    I would do:

    var err = [fetch('index.html').then((success) => { return Promise.resolve(success); }).catch((e) => { return Promise.resolve(e); }),
    fetch('http://does-not-exist').then((success) => { return Promise.resolve(success); }).catch((e) => { return Promise.resolve(e); })];
    
    Promise.all(err)
    .then(function (res) { console.log('success', res) })
    .catch(function (err) { console.log('error', err) }) //never executed
    

提交回复
热议问题