promise請求數據(all方法)


ReadData(context){
      // 第一個promise開始
      var promise1 = new Promise((resolve,reject)=>{
        axios.get("http://localhost:8080/api/fenlei.json").then((response)=>{
          // console.log(response.data)
          resolve(response.data)
        }).catch((error)=>{
          reject(error)
          console.log(error)
        })
      }).then((data)=>{
        context.commit("readJson",data)
      }).catch((error)=>{
        console.log(error)
      })
      // 第一個promise結束
      // 第二個promise開始
      var promise2 = new Promise((resolve,reject)=>{
        axios.get("http://localhost:8080/api/list.json").then((response)=>{
          resolve(response.data)
        }).catch((error)=>{
          console.log(error)
          reject(error)
        })
      }).then((data)=>{
        context.commit("ReadlistJson",data)
      }).catch((error)=>{
        console.log(error)
      })
      //第二個結束

     //全部完成以后,統一進行操作,為了防止多個請求有沒有完成的,影響下一步操作。
      Promise.all([promise1,promise2]).then(()=>{
        context.commit("SortItems")
      }).catch((error)=>{
        console.log(error)
      })
    }

  


免責聲明!

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



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