axios multiple concurrent requests

  

Scenes:

  Click the Export button in Excel to achieve, all the articles in the first five data list of names sent to the back-end, the list of names clicked data will be stored in localStorage in;

Ideas:

  Click the Export button, the first five-by-data and comparison of the data in the localStorage, find the data in the first five did not send the request, into the array, and then these data are not requested to conduct a multiple concurrent requests to multiple concurrent requests data stored in the browser, and then unified by name the top five in order to pull data localStorage, submit background;

problem:

  How to send multiple requests?

Solution:

  method one:

  

getNameData(name,affiliation){
    return this.$axios.get("/api/academic/paper_search", {
          params: {
             name: name,
             affiliation: affiliation
          }
       })
    },
 sendAllAxios(){
       let arr = []
       this.getFiveNameData.forEach(item => {
       if(!JSON.parse(localStorage.getItem("baseInfo"))[item.name]){
           arr.push( this.getNameData(item.name,item.affiliation))
          }
        });
       return new Promise((resolve,reject)=>{
            if(arr.length){
                this.$axios.all(arr).then((res)=>{
                res.forEach(item=>{
                   if(item.status == 200){
                       this.baseInfo[item.config.params.name] = item.data
                    }
                     })
                localStorage.setItem("baseInfo",JSON.stringify(this.baseInfo))                            
                resolve()
            }).catch(e=>{console.log(e)})
         }else{
            let sendData = {}
            this.getFiveNameData.forEach(item => {
            sendData[item.name] = JSON.parse(localStorage.getItem("baseInfo"))[item.name]
          })
             resolve(sendData)
         }
       })     
     },
            

Method Two:

  Promise.all(arr).then(res=>{ })

Stepped pit:

 . This $ axios.all (arr) .then ((res) => {}) then the first parameter is an array, the information indicating that all requests;

 the this. $ axios.all (ARR) .then (the this. $ {}) in this manner is in the form of a data request to return data individually expanded, acct represents a request to return the first data array, perms indicates the second data,axios.spread(function (acct, perms)

note:

  spread is determined in the case of several requests issued together

 

 

 

Guess you like

Origin www.cnblogs.com/zmdblog/p/11240226.html