Promise.all find which promise rejected

为君一笑 提交于 2019-12-20 19:48:39

问题


In my code, I am using Promise.all() to run code asynchronously once some promises have all fulfilled. Sometimes, one promise will fail, and I'm not sure why. I would like to know which promise is failing. Passing a callback as a second parameter to the .then method does not help much, as I know that a promise is rejecting but not which promise is rejecting.

A stack trace does not help either, as the first item is the Promise.all()'s error handler. A line number from the Error object passed to the first parameter of the second function passed to the try function of the Promise.all() is simply the line number of the line where I log the line number.

Does anybody know of a way to find out which promise is rejecting?


回答1:


You can use an onreject handler on each promise:

Promise.all(promises.map((promise, i) =>
    promise.catch(err => {
        err.index = i;
        throw err;
    });
)).then(results => {
    console.log("everything worked fine, I got ", results);
}, err => {
    console.error("promise No "+err.index+" failed with ", err);
});

In general, every rejection reason should contain enough information to identify the issue that you need to handle (or log).




回答2:


this wrapper will wait for and return every result and/or rejection

the returned array will be objects

{ // this one resolved
    ok: true,
    value: 'original resolved value'
},
{ // this one rejected
    ok: false,
    error: 'original rejected value'
},
{ // this one resolved
    ok: true,
    value: 'original resolved value'
},
// etc etc

One caveat: this will wait for ALL promises to resolve or reject, not reject as soon as the first rejection occurs

let allresults = function(arr) {
    return Promise.all(arr.map(item => (typeof item.then == 'function' ? item.then : Promsie.resolve(item))(value => ({value, ok:true}), error => ({error, ok:false}))));
}

allresults(arrayOfPromises)
.then(results => {
    results.forEach(result => {
        if(result.ok) {
            //good
            doThingsWith(result.value);
        } else {
            // bad
            reportError(result.error);
        }
    });
});


来源:https://stackoverflow.com/questions/42008227/promise-all-find-which-promise-rejected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!