axios同時發送多個或不定量的請求,並等待所有請求完畢后操作


  • 同時發送多個請求

      Axios.all([request1, request2, request3])
          .then(
            Axios.spread((res1, res2, res3) => {
              console.log('全部加載完成')
            })
          )
          .catch(err => {
            console.log(err.response)
          });
    

    Axios.spread中的函數,請求全部完成后會調用,並且請求數據會一一對應參數。

    發送的請求數不確定時

    使用map結合Axios.all

      Axios.all(arr.map(function (data)=>{
      	return this.axios.post(....)
      }))
          .then(
            Axios.spread((...a) => {
              console.log('全部加載完成')
            })
          )
          .catch(err => {
            console.log(err.response)
          });
    

arr是會靈活變化的數組,根據map方法返回多個promise。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM