async、await及Promise.all()、Promise.race()

Promise:

Promise提供了一种新的更优雅的异步编程方式,结合async/await,可以将Promise的链式调用变为类似同步代码的形式。

            function 接口1(){
				return new Promise((resolve, reject) => {
					console.log(1)
					setTimeout(()=>{
						resolve("成功 接口1")
					},1000)
					
					// reject("失败")
				});
			}
			function 接口2(){
				return new Promise((resolve, reject) => {
					console.log(2)
					setTimeout(()=>{
						resolve("成功 接口2")
					},2000)
					// reject("失败")
				});
			}
			function 接口3(){
				return new Promise((resolve, reject) => {
					console.log(3)
					setTimeout(()=>{
						resolve("成功 接口3")
					},500)
					// reject("失败")
				});
			}

async、await的使用:

async function  testAsyncAwait(){
				// let result = null;
				// 接口1().then(res=>{
				// 	// result = res
				// 	接口2().then((re)=>{
				// 		接口3().then((r)=>{
				// 			console.log(r)
				// 		})
				// 	})
				// 	console.log(result,29)
				// }).catch(err=>{
				// 	console.log(err)
				// 	result = err
				// });
				// p2(result)
				// //  很多时候  我们需要 得到接口返回的数据 然后拿着接口的返回数据 去调用其他的方法做二次处理 
				let jiekou1 =  await 接口1()
				
				let jiekou2 =  await 接口2()
				
				let jiekou3 =  await 接口3()
				
				let jiekou1 =  await 接口1()
				
				let jiekou2 =  await 接口2()
				
				let jiekou3 =  await 接口3()
				// console.log(jiekou1,55)
				// console.log(jiekou2,55)
				// console.log(jiekou3,55)
				// p2()
				//  总结  await 接收的 结果 如果是 promise的失败接口 那么await下面的代码 会不执行  因为 await 需要接收到的promise结果 是成功 
				
				
				// let arr = [接口1(),接口2(),接口3()]
				// 高仿PromiseAll(arr).then((res)=>{
				// 	console.log(res)//["失败","失败","失败"]
				// }).catch((err)=>{
				// 	console.log(err)
				// })
				// Promise.all(arr).then((res)=>{
				// 	console.log(res)//["失败","失败","失败"]
				// }).catch((err)=>{
				// 	console.log(err)
				// })
				// 面试中 经常问 说 我有3个接口  然后  我想用promise.all 但是 我不管他是成功 还是失败 我都要得到一个结果数组 
				// Promise.race(arr).then((res)=>{
				// 	console.log(res,87)
				// })
			}

总结:

 await 接收的 结果 如果是 promise的失败接口 那么await下面的代码 会不执行  因为 await 需要接收到的promise结果 是成功

Promise.all()的使用:

async function  testAsyncAwait(){
				// let result = null;
				// 接口1().then(res=>{
				// 	// result = res
				// 	接口2().then((re)=>{
				// 		接口3().then((r)=>{
				// 			console.log(r)
				// 		})
				// 	})
				// 	console.log(result,29)
				// }).catch(err=>{
				// 	console.log(err)
				// 	result = err
				// });
				// p2(result)
				// //  很多时候  我们需要 得到接口返回的数据 然后拿着接口的返回数据 去调用其他的方法做二次处理 
				// let jiekou1 =  await 接口1()
				
				// let jiekou2 =  await 接口2()
				
				// let jiekou3 =  await 接口3()
				
				// let jiekou1 =  await 接口1()
				
				// let jiekou2 =  await 接口2()
				
				// let jiekou3 =  await 接口3()
				// console.log(jiekou1,55)
				// console.log(jiekou2,55)
				// console.log(jiekou3,55)
				// p2()
				//  总结  await 接收的 结果 如果是 promise的失败接口 那么await下面的代码 会不执行  因为 await 需要接收到的promise结果 是成功 
				
				
				let arr = [接口1(),接口2(),接口3()]
				// 高仿PromiseAll(arr).then((res)=>{
				// 	console.log(res)//["失败","失败","失败"]
				// }).catch((err)=>{
				// 	console.log(err)
				// })
				Promise.all(arr).then((res)=>{
					console.log(res)//["失败","失败","失败"]
				}).catch((err)=>{
					console.log(err)
				})
				// 面试中 经常问 说 我有3个接口  然后  我想用promise.all 但是 我不管他是成功 还是失败 我都要得到一个结果数组 
				// Promise.race(arr).then((res)=>{
				// 	console.log(res,87)
				// })
			}

总结:

promise all 他接收的 是一个 promise的 数组  然后 一旦 数组中出现一个失败 那么 都为失败

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值