vue同步请求

本文详细介绍了Vue中如何实现同步请求,通过async/await语法糖,使得异步操作可以按照预期顺序执行。分别展示了没有同步请求、部分同步请求和全部同步请求的执行效果,帮助开发者更好地理解和掌握在Vue中进行同步调用的方法。
摘要由CSDN通过智能技术生成

原文地址:vue 同步请求_Aa_duidui的博客-CSDN博客_vue同步请求

同步请求执行的顺序
async-await 挂上的才是同步,没挂上的还是异步
async 方法名(){
    await 请求方法(参数).then(res=>{
        //xxx
    });
}
//或者
async 方法名(){
    var res = await 请求方法(参数);
    //xxx
}
调用同步的方法,会有以下几种情况

1.没有同步(异步请求)

        请求结果
        111
        333
        444
        222

f1(){
    console.log(111)
    this.f2(id);
    console.log(444);
}
 
f2(id){
    r1(id).then(res={
        console.log(222);
    })
    console.log(333);
}
2.调用的方法同步请求

        打印结果
        111
        444
        222
        333

f1(){
    console.log(111)
    this.f2(id)
    console.log(444)
}
async f2(id){
    await c1(id).then(res=>{
        conole.log(222);
    })
    console.log(333);
}
3.所有方法全是同步

        打印结果
        111
        222
        333
        444

async f1(){
    console.log(111)
    await this.f2(id)
    console.log(444)
}
async f2(id){
    await c1(id).then(res=>{
        conole.log(222);
    })
    console.log(333);
}
一个方法里面多个需要同步请求
async f1(){
    var v1 = await this.c1(id);
    var v2 = await this.c2(v2.id);
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值