请问两个关于接口调用的问题,谢谢大家?

2023-06-27 307 0

image.png

that.submit_order_info();
that.submit_order_info().then(res => {
  console.log('啊啊啊' ,res)
     that.get_check_submit_order()
})

我想等submit_order_info 这个接口调用后,再执行that.get_check_submit_order() 这个接口的调用,

请问如何去修改?

两种方法:
一种使用promise.then

const submit_order_info = () => {
        return new Promise((resolve, reject) => {
          ...
          resolve()
        })
      }
      that.submit_order_info().then(res => {
        that.get_check_submit_order()
      })

还有一种用async await

const test = async () => {
        let res = that.submit_order_info()
        that.get_check_submit_order()
      }

就如同OP你写的一样直接写在 submit_order_info.then() 当中。

fn(){
  that.submit_order_info().then(res => {
    that.get_check_submit_order().then(res2 = >{
      ...
    })
  })
}

或者改写成 async/await 的方式。

async fn(){
  ...
  // 如果需要返回值
  const orderInfo = await that.submit_order_info()
  // 如果不需要返回值
  await that.submit_order_info()
  const checkInfo = await that.get_check_submit_order()
  ...
}

使用async...await处理。

async init() {
   await this.submit_order_info();
   await this.get_check_submit_order();
}

回答

相关文章

nuxt2部署静态化和ssr的时候访问首页先报404再出现首页为什么?
`clip-path` 如何绘制圆角平行四边形呢?
多线程wait方法报错?
VUE 绑定的方法如何直接使用外部函数?
vue2固定定位该怎么做?
谁有redis实现信号量的代码,希望借鉴一下?