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