当前位置:网站首页>多接口调用,使用Promise.all、Promise.race和Promise.any
多接口调用,使用Promise.all、Promise.race和Promise.any
2022-06-25 22:56:00 【清风细雨_林木木】
大纲
1.Promise.all
2.Promise.race
3.Promise.any
1.Promise.all 使用
说明:Promise.all可以将多个Promise实例包装成一个新的Promise实例。同时,成功和失败的返回值是不同的,成功的时候返回的是一个结果数组,而失败的时候则返回最先被reject失败状态的值。
实例:
let p1 = new Promise((resolve, reject) => {
resolve('成功1')
})
let p2 = new Promise((resolve, reject) => {
resolve('成功2')
})
let p3 = new Promise((resolve, reject) => {
reject(new Error('失败')) // 直接写错误ESLint报错,需要reject(new Error(0))写法
})
Promise.all([p1, p2]).then((result) => {
console.log(result, '成功1--')
}).catch((error) => {
console.log(error, '失败')
})
Promise.all([p1, p2, p3]).then((result) => {
console.log(result, '成功2--')
}).catch((error) => {
console.log(error)
})
结果:
Promise.all获得的成功,数组里面的数据顺序和Promise.all接收到的数组顺序是一致的;多个请求数据的过程中,按照顺序获取和使用数据的场景;失败的时候则返回最先被reject失败状态。
打印结果:

2.Promise.race 使用
说明:Promse.race 意思就是说,Promise.race([p1, p2, p3])里面哪个结果获得的快,就返回那个结果,不管结果本身是成功状态还是失败状态。
let p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('success')
},1000)
})
let p2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('失败'))
}, 500)
})
Promise.race([p1, p2]).then((result) => {
console.log(result)
}).catch((error) => {
console.log(error) // 打开的是 'failed'
})
结果:
3.Promise.any
说明:Promise.any() 方法同样是将多个 Promise 实例,包装成一个新的 Promise 实例。Promise.any() 有一个子实例成功就算成功,全部子实例失败才算失败。
边栏推荐
- C#使用MySql进行操作
- Analyze the five root causes of product development failure
- Law and self-regulation in the meta universe
- Px4 system terminal for pixhawk
- Motor monitoring system based on MCGS and stm32
- 安卓缓存使用工具类
- Data synchronization
- Things / phenomena / things / things / situations / appearances
- 4 key points to help the product manage the project well
- Preorder and middle order traversal of forest
猜你喜欢

Preorder and middle order traversal of forest

模板引擎——FreeMarker初体验

CXF

jarvisoj_level2_x64

Apache foundation officially announced Apache inlong as a top-level project

Modelsim simulation FFT core cannot be simulated solution (qsys)

ciscn_2019_en_2

随便画画的

Msp430f5529lp official board (red) can not debug the problem

Atlas200dk刷机
随机推荐
渗透工具-Burpsuite
Phoenix index
Endnote IEEE TRANSACTIONS ON INDUSTRIAL ELECTRONICS/TIE/TPEL 参考文献格式模板
案例:绘制Matplotlib动态图
Megacli common command collation
debezium
How to design the product roadmap?
C thread pool control semaphore
Atlas200dk brush machine
Openresty chapter 01 introduction and installation configuration
Why is it best to use equals for integer comparisons
Anaconda beginner's notes
Android cache usage tool class
使用VS2022編譯Telegram桌面端(tdesktop)
11.1.1 overview of Flink_ Flink overview
Chapter VI exercises (678) [microcomputer principles] [exercises]
Flex & Bison 开始
[understanding of opportunity -30]: Guiguzi - internal "chapter - empathy, stand on the other side's position and narrow the psychological distance with the other side
Download and install flume
Motor monitoring system based on MCGS and stm32