10、兩個異步請求函數,如何使用async和await改變執行順序,實現結果傳遞?


要點:1、async、await 結合promise一起使用
     1、async應該要放在第一個異步函數的then回調里 then(async(res) => {})
     2、需要await的函數,采用new Promise(resolve,reject)的resolve(res)將執行結果傳遞出去
     3、需要await的函數調用后會返回一個promise,再通過then(res),即可獲得上面resolve的結果 此處是重點!!!
     
 完整代碼:
      <s-table ref="table" :columns="columns" :data="loadData" :alert="false" :rowKey="(record) => record.id">
          <span slot="opTime" slot-scope="text">
            <ellipsis
              :length="24"
              tooltip
              style="font-weight:bold"
              :class="text.includes(5)?'green':'red'">{{ text }}</ellipsis>
          </span>

           <span slot="objectNatureScopedSlots" slot-scope="text">
            {{ 'duiXiangXingZhi' | dictType(text) }}
          </span>

          <span slot="action" slot-scope="text, record">
            <span slot="action">
              <a @click="tableClick(record.taskCode)">
                <img src="@/assets/image/eye.png" alt="" srcset=""/>
              </a>
            </span>
          </span>
        </s-table>

 data(){
   return{
       // 加載數據方法 必須為 Promise 對象
        loadData: (parameter) => {
          return bussinessEscortBaseInfoPage(Object.assign(parameter, this.queryParam)).then(async(res) => {
            console.log('res.data',res.data)
            var taskArr=[]
            if(res.data.rows.length>0){
              res.data.rows.forEach((item,index)=>{
                console.log('item.taskCode',item.taskCode)
                taskArr.push(item.taskCode)
              })          
               this.task=taskArr.toString()
              await this.queryGrade(this.task).then(result=>{
                 result.forEach((item,index)=>{
                  if(item==null){
                    res.data.rows[index]={
                      ...res.data.rows[index],
                      departureTime:res.data.rows[index].departureTime.slice(0,10),
                      timeOfArrival:res.data.rows[index].timeOfArrival.slice(0,10),
                      grade:'無'
                    }
                  }else{
                    res.data.rows[index]={
                      ...res.data.rows[index],
                      departureTime:res.data.rows[index].departureTime.slice(0,10),
                      timeOfArrival:res.data.rows[index].timeOfArrival.slice(0,10),
                      grade:item.grade
                    }
                  }
                })
                console.log('res.data----',res.data) 
              }) 
            }
            return res.data
          })
        },
      
   }
 },
 methods:{
     //查詢評價等級
      queryGrade(task){    
        return new Promise((resolve,reject)=>{
          var data={
            taskCode:task
          }
          businessSuperViseInspectionGrade(data).then(res=>{
            if(res.success){
              resolve(res.data)
            }else{
              this.$message.error('督導結果查詢失敗'+res.message)
              reject()
            }  
          })
        })
        
      },
 }

 

 


免責聲明!

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



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