react 中使用async和await

执行addCompany但val没拿到,怎么能解决,别说放回调里面

handleButton =()=>{
   var val
   this.getImgIdByFileSaveList(arrImg).then((res)=>{
      val =res
      console.log('先执行',val)
    })
   this.addCompany(val)
}

addCompany = async (params)=>{
    await axios.ajax({
      url:api.addCompany,
      method:"post",
      data:params
    }).then((res)=>{
        console.log('后执行')
    })
  }
  getImgIdByFileSaveList  = async (val)=>{
    return await new Promise((resolve)=>{
       axios.ajax({
        url:api.getImgIdByFileSaveList,
        method:"post",
        data:{
          fileSaveList:val
        }
      }).then((res)=>{
        resolve(res.data)
      })
    })
  }

阅读 14.3k
2 个回答

await 只能在 async 函数中使用。建议先了解了用法再使用async

handleButton = async()=>{
var val =await this.getImgIdByFileSaveList(arrImg)
await this.addCompany(val)
}

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题