How vue-cli axios.all makes requests execute sequentially

this.axios.all([this.submitPro(),this.InvitationPro()]).then(this.axios.spread((res1,res2)=>{
   console.log(res1)
   console.log(res2)
}))
InvitationPro() {
  this.axios.post("/api/app/json", {
                    "header": {
                        "bizCode": "staff009"
                    },
                    "body": {
                        "beInvitedCode": "100139" //:String    :
                    }
                })
            },
            submitPro() {
                this.axios.post("/api/app/json", {
                    "header": {
                        "bizCode": "staff003"
                    },
                    "body": {
                        "staffCode": this.info.buyName,
                        "phoneCode": this.info.verCode, 
                        "captchaCode": this.info.randomCode, 
                        "staffPassword": this.info.lognPsd, 
                        "provinceCode": this.temp.addressCode__province,
                        "cityCode": this.temp.addressCode__city, 
                        "countyCode": this.temp.addressCode__dist,
                        "beInvitedCode": this.info.inviteCode 
                    }
                })
            },

I want the request this.InvitationPro () to be executed first and then this.submitPro (), and then I found that await is not very good at using

.
Jun.11,2022

There are also many

methods. Here's a brief description:

1. A relatively simple way is to wait for one request to come before initiating the next request:

this.invitationPro().then(() => {
    this.submitPro().then(() => {
        this.otherRequest();
    });
});

it will fall into callback hell. You can do it in another way:

this.invitationPro()
    .then(this.submitPro)
    .then(this.otherRequest)

2, the second is to use ES6's async...await syntax sugar:

async function subData() {
    const initRes = await this.invitationPro();
    const subRes = await this. submitPro();
    const res = await this.otherRequest();
    // other action
}

subData();

await is the syntax sugar of Generator (it can also be implemented using Generator). It is important to note that await is followed by a Promise object, which may be reject. If you need exception handling, you'd better put it in try-catch.

3, for...of

borrow an example from Ruan Yifeng's document, without going into details:

async function logInOrder(urls) {
  for (const url of urls) {
    const response = await fetch(url);
    console.log(await response.text());
  }
}

to sum up so much for the time being, the landlord can choose an appropriate way


http://es6.ruanyifeng.com/-sharpdo...

Menu