admin管理员组文章数量:1245133
I am trying to run a for loop which queues a bunch of asynchronous requests. Once all the requests plete, independent of whether they resolve
or reject
, I want to then run some code. I am trying to take advantage of the async/await pattern, as it looks nicer. :)
This is what I am doing:
var promises = []
for ( item in list ) {
prom = AsyncFunction( item )
promises.push(prom)
}
await Promise.all(promises)
doMoreAfter()
However, some of the promises fail, and the second that happens Promise.all()
fails aswell.
I want to simply ignore any failed promises, and have the next code run after all the promises have pleted.
I found solutions like this.
Promise.all([a(), b(), c()].map(p => p.catch(e => e)))
.then(results => console.log(results)) // 1,Error: 2,3
.catch(e => console.log(e));
But it doesn't look like it works when trying to turn it into the async/await format.
await Promise.all(promises.map(p => p.catch(e => e)))
What am I missing?
I am trying to run a for loop which queues a bunch of asynchronous requests. Once all the requests plete, independent of whether they resolve
or reject
, I want to then run some code. I am trying to take advantage of the async/await pattern, as it looks nicer. :)
This is what I am doing:
var promises = []
for ( item in list ) {
prom = AsyncFunction( item )
promises.push(prom)
}
await Promise.all(promises)
doMoreAfter()
However, some of the promises fail, and the second that happens Promise.all()
fails aswell.
I want to simply ignore any failed promises, and have the next code run after all the promises have pleted.
I found solutions like this.
Promise.all([a(), b(), c()].map(p => p.catch(e => e)))
.then(results => console.log(results)) // 1,Error: 2,3
.catch(e => console.log(e));
But it doesn't look like it works when trying to turn it into the async/await format.
await Promise.all(promises.map(p => p.catch(e => e)))
What am I missing?
Share Improve this question edited Nov 26, 2017 at 1:58 Shawn Tabrizi asked Nov 26, 2017 at 1:51 Shawn TabriziShawn Tabrizi 12.4k2 gold badges42 silver badges73 bronze badges 1-
Well,
Promise.all()
rejects as soon as the first promise you pass to it rejects so the ONLY way to make it wait for all promises to finish is to keep any promise you pass toPromise.all()
from rejecting. When I want this type of behavior, I just use on of thesePromise.settle()
versions which gives you the desired behavior built-in to a utility function rather than hand coded separately each time. – jfriend00 Commented Nov 26, 2017 at 6:44
3 Answers
Reset to default 12I feel like the best you can do is this:
var promises = [a, b, c];
promises = promises.map(p => p().catch(e => undefined));
values = await Promise.all(promises);
console.log(values); // ["a", undefined, "c"]
https://jsfiddle/DerekL/h5Lmxaqq/
Or you can create an ignore
function:
function ignore(promise){
return promise.catch(e => undefined);
}
async function main(){
var promises = [a(), b(), c()];
var values = await Promise.all(promises.map(ignore));
console.log(values);
}
https://jsfiddle/DerekL/mwkww7w9/
I tried running your example like this: https://jsfiddle/804ogadu/3/
var a = Promise.resolve('a');
var b = Promise.resolve('b')
var c = Promise.reject('errc');
async function test(){
const t = await Promise.all([a, b, c].map(p => p.catch(e => e)));
console.log(t);
}
test();
I do get the required result
['a,'b','errc']
Did you consider using Promise.allSettled()
? (https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled#using_promise.allsettled)
本文标签: How to ignore errors using asyncawait syntax in JavaScript Promiseall()Stack Overflow
版权声明:本文标题:How to ignore errors using asyncawait syntax in JavaScript Promise.all() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740237587a2246754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论