promise 同步执行异步请求 内附代码 写法求教

shopSum(){
    return new Promise(resolve=>{
      resolve();
      this.$post('ord003', {
        "flag":true
      }).then(res => {
          //请求1 
      })
    })
}
activitySum(){
   return new Promise(resolve=>{
      resolve();
      this.$post('ord003', {
        "flag":true
      }).then(res => {
          //请求2 
      })
    })
}
orderySum(){
   return new Promise(resolve=>{
      resolve();
      this.$post('ord003', {
        "flag":true
      }).then(res => {
          //请求3 
      })
    })
}
shopSum()
.then(activitySum())
.then(orderySum())

这样写他并没有按照顺序 求教

阅读 2.3k
1 个回答

首先shopSum、activitySum、orderySum中都要return一个promise,不知道你里面具体是怎么写的
然后像下面这样写

showSum()
.then(activitySum)
.then(ordeySum)

然后好好去学习一下promise,比较推荐mdn的资料,如果有能力尽量去读英文版的,promise本质上是callback的封装,promise学明白了你的问题自然就解决了
https://developer.mozilla.org...
https://developer.mozilla.org...


像这样改

shopSum(){
    return new Promise(resolve=>{
      this.$post('ord003', {
        "flag":true
      }).then(res => {
          resolve();
          //请求1
      })
    });
}

或是更简单一点

shopSum(){
    return this.$post('ord003', {
        "flag":true
      }).then(res => {
          //请求1 
      })
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题