Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

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())

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

首先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 
      })
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...