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(()=>{

	    });
    },
}

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐