Wait until all promises complete even if some rejected

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

    var err;
    Promise.all([
        promiseOne().catch(function(error) { err = error;}),
        promiseTwo().catch(function(error) { err = error;})
    ]).then(function() {
        if (err) {
            throw err;
        }
    });
    

    The Promise.all will swallow any rejected promise and store the error in a variable, so it will return when all of the promises have resolved. Then you can re-throw the error out, or do whatever. In this way, I guess you would get out the last rejection instead of the first one.

提交回复
热议问题