同步请求执行的顺序

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);
}

Logo

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

更多推荐