【axios】 实现同步调用

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

	    });
    },
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值