-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promise.all 的错误捕获 #73
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Promise.all
的错误捕获在开发中,大多数时间都是一个异步操作一个异步操作去执行的,但是有一些特殊情况,需要一股脑去执行多个异步操作,比如:
这时候就会用到
Promise.all
这个方法。❇️
Promise.all
从
Promise.all
的运行中我们发现,若 promises 中有一个请求出错的话,整体 promises 就会被中断且只会输出这个失败的状态,而忽略了其他成功的值。假如我们不希望某种错误影响到并行请求的结果输出时,就需要对Promise.all
做一层静默处理:✅ 方法一:在传入 promises 前,先对 promises 中的每个 promise 对象都优先捕获自身的错误,防止错误向外传播到 Promise.all 方法内。
✅ 方法二:在ES2020中,JavaScript提供了一个新语法
Promise.allSettled
,他能直接完成我们上面所做的处理Promise.allSettled
The text was updated successfully, but these errors were encountered: