当前位置:网站首页>一定要执行多个请求,都要捕获错误,使用try catch 不够优雅
一定要执行多个请求,都要捕获错误,使用try catch 不够优雅
2022-07-23 16:53:00 【zeuskk】
捕获错误刚开始我是这样写的
这样写倒是没问题都能捕获到每一个错误,但是这么多的try catch 看起来不够优雅
try{
const a = await getList1();
setStateA(a?.items);
} catch(error){
Toast.success("出错了!", error)
}
try{
const b = await getList2();
setStateB(b?.items);
} catch(error){
Toast.success("出错了!", error)
}
try{
const c = await getList3();
setStateC(c?.items);
} catch(error){
Toast.success("出错了!", error)
}
...
然后我就灵机一动
try{
const a = await getList1();
const b = await getList2();
const c = await getList3();
setStateA(a?.items);
setStateB(b?.items);
setStateC(c?.items);
} catch(error){
Toast.success("出错了!", error)
}
- 可是这样写会出现一个问题,当第一个请求
a报错, 后面的都不会执行。
再灵机一动
try {
const responses = await Promise.all([
getList1(),
getList2(),
getList3(),
])
setStateA(responses[0]);
setStateB(responses[1]);
setStateC(responses[2]);
}
catch(error) {
Toast.success("出错了!", error)
}
今天的分享就到这里吧,不能算是分享,小记小记
边栏推荐
- Common problems of sklearn classifier
- LM393低功耗双电压比较器参数、引脚、应用详解
- ROS (27): the simple use of rosparam and the unsuccessful transfer of parameters through launch and its solution
- Great God "magic change" airpods, equipped with usb-c interface, 3D printing shell makes maintenance easier
- 什么是堆栈以及堆栈的区别
- 多线程与高并发day11
- Electronic components - resistance
- 【重磅】聚焦券商终端业务,博睿数据发布新一代券商终端核心业务体验可观测平台
- Implementation of SPI communication protocol based on FPGA
- 11. Basic concepts of neural network
猜你喜欢

LM393 low power dual voltage comparator parameters, pins, application details

Building virtual private network based on softther

How to become a modeler? Which is more popular, industrial modeling or game modeling?
![[2022] [paper notes] terahertz quantum well——](/img/05/27e9b6a5b2aebf8aa4604f5992551e.png)
[2022] [paper notes] terahertz quantum well——

【2018】【论文笔记】石墨烯场效应管及【2】——石墨烯的制备、转移

【重磅】聚焦券商终端业务,博睿数据发布新一代券商终端核心业务体验可观测平台
![Log framework [detailed learning]](/img/2f/2aba5d48e8a544eae0df763d458e84.png)
Log framework [detailed learning]

Three things programmers want to do most | comics

The first layer of OSI model: physical layer, the cornerstone of existence!

Detailed explanation: tmp1750 chip three channel linear LED driver
随机推荐
Error reporting caused by the use of responsebodyadvice interface and its solution
C#Split的用法,Split分割字符串
什么是堆栈以及堆栈的区别
The first layer of OSI model: physical layer, the cornerstone of existence!
怎么将word中的times new roman的双引号替换成宋体双引号
?前台传参的问题待确认
LM393 low power dual voltage comparator parameters, pins, application details
多线程【全面学习 图文精讲】
[2022] [paper notes] terahertz quantum well——
迷宫类dp整合
ThreadPoolExecutor源码分析
What does MySQL access port mean_ What is the port number of the database port
? The problem of front desk parameter transmission needs to be confirmed
Analysis on the implementation of Flink exactly once delivery
Electronic components - resistance
【论文阅读】GETNext: Trajectory Flow Map Enhanced Transformer for Next POI Recommendation
ZigBee集成开发环境IAR安装
Understanding and simple implementation of time2vec
Know two things: how does redis realize inventory deduction and prevent oversold?
C # split usage, split split split string