自己总结的:
1、让异步的代码可以使用同步的方式,逻辑更加清晰,优雅
2、重点:基于promise使用
模拟场景:调用A接口的同时返回数据后在次调用B接口获取数据
export default {
async created () {
// 使用 promise 方式
// 调用A
// this.$http.get('http://localhost:3000/a').then(res => {
// console.log(res.data)
// // 调用B
// return this.$http.get('http://localhost:3000/b')
// }).then(res => {
// console.log(res.data)
// })
// 函数的返回值 加载await之后 是then接受的数据
// 在使用await之后在 外层函数必须用async 来申明
const resA = await this.$http.get('http://localhost:3000/a')
const resB = await this.$http.get('http://localhost:3000/b')
console.log(resA.data, resB.data)
}
总结:
-
-
awiat 使用只能在async修饰的函数内,在awiat的外层函数加上async关键字。
-
awiat 修饰的函数 只会阻碍程序运行的,async关键字修饰函数 异步函数。
浙公网安备 33010602011771号