async 与 await 配置使用实现同步
methods: {
async funA(){
var res = await axios.post('xxx');
console.log('axios执行完毕才打印');
}
}
注意:这样的方式只限于使用在axios通信的那一层,如果对axios再包一层函数,然后对这外一层函数使用这样的方式修饰同步,无法实现同步效果
吐槽:这样的话。。那不是跟我等响应数据回来再处理一样么。。
methods: {
async funA(){
axios.get(url).then((response)=>{
console.log(response);
console.log('axios执行完毕才打印');
}).catch((err)=>{
console.log(err);
}).finally(()=>{
});
}
}
解决:
调用方和被调用方都要使用async 与 await 修饰
methods:{
async function01(){
await this.function02();
console.log('function02调用完毕后打印');
},
async function02(){
let url = this.url.list;
await axios.get(url).then((response)=>{
console.log(response);
console.log('数据回传xxxx');
}).catch((err)=>{
console.log(err);
}).finally(()=>{
});
},
}
—————`———————————
版权声明:本文为优快云博主「Peko」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/piano_diano/article/details/115899481