当前位置:网站首页>一定要执行多个请求,都要捕获错误,使用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)
}
今天的分享就到这里吧,不能算是分享,小记小记
边栏推荐
- 【ONNX】动态输入尺寸的问题(多输出/多输入)
- 多线程&高并发(全网最新:面试题+导图+笔记)面试手稳心不慌
- Redis【2022最新面试题】
- 【重磅】聚焦券商终端业务,博睿数据发布新一代券商终端核心业务体验可观测平台
- Leetcode sword finger offer II 115. reconstruction sequence: diagram topology sorting
- Storage structure and method of graph (II)
- SQL statement exercise
- Common problems of sklearn classifier
- 【2022】【论文笔记】太赫兹量子阱——
- [2020] [paper notes] Based on Rydberg atom——
猜你喜欢

LM393 low power dual voltage comparator parameters, pins, application details
![Redis [super superfine introductory tutorial]](/img/a6/02b3e670067292d884bab7425b4ffb.png)
Redis [super superfine introductory tutorial]
![Gradle [graphic installation and use demonstration]](/img/e4/ac69e490fd06ed4ad6a90972273373.png)
Gradle [graphic installation and use demonstration]

【2013】【论文笔记】太赫兹波段纳米颗粒表面增强拉曼——

How to replace the double quotation marks of Times New Roman in word with the double quotation marks in Tahoma

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

Flink Exactly-Once 投递实现浅析
![[attack and defense world web] difficulty four-star 12 point advanced question: cat](/img/fc/6508c7d534c26f487b0a8ae5be7347.png)
[attack and defense world web] difficulty four-star 12 point advanced question: cat

【2020】【论文笔记】基于二维光子晶体的光控分光比可调Y——
![[2020] [paper notes] new terahertz detection - Introduction to terahertz characteristics, various terahertz detectors](/img/94/ff67867ef3237d8779628c8872b694.png)
[2020] [paper notes] new terahertz detection - Introduction to terahertz characteristics, various terahertz detectors
随机推荐
MySQL [knowing and mastering one article is enough]
My creation anniversary
jumpserver管理员账号被锁定
[2018] [paper notes] graphene FET and [2] - Preparation and transfer of graphene
The first layer of OSI model: physical layer, the cornerstone of existence!
What does MySQL access port mean_ What is the port number of the database port
Paddlenlp之UIE分类模型【以情感倾向分析新闻分类为例】含智能标注方案)
Terminal终端命令(全)
[2020] [paper notes] phase change materials and Hypersurfaces——
Navigation component of jetpack compose uses
LM393低功耗双电压比较器参数、引脚、应用详解
[attack and defense world web] difficulty Samsung 9-point introductory question (end): Fakebook, favorite_ number
Google正在改进所有产品中的肤色表现 践行“图像公平”理念
多线程【全面学习 图文精讲】
Storage structure and method of graph (II)
Multithreading and high concurrency Day11
SQL 语句练习
Handwriting bind, call, apply is actually very simple
Deepstream learning notes (II): description of GStreamer and deepstream-test1
LeetCode 剑指 Offer II 115.重建序列:图解 - 拓扑排序