多个Promise执行顺序

app.isLogin()
    // 判断是否登录后
    .then(res=>{
        this.setData({
          login: true
        }, res2=>{
          // 清空临时积分
          return app.clearTempScore()   // 返回Promise
        })
    })
    .then(res => {
      console.log('.then..............')
    })
    .catch(err=>{
      console.log(err)
    })

1、setData中返回Promise

   会直接执行第二个.then(),即使app.clearTempScore返回的状态是pending(正常返回的Promise,状态是pending,不会执行.then())

   因为setData是异步请求,会拿到 临时储物台 执行,此时,流水线上并没有 返回 Promise

   所以,会顺序执行流水线上的第二个.then()

app.isLogin()
    // 判断是否登录后
    .then(res=>{
        
    })
    .then(res => {
      console.log('.then..............')
    })
    .catch(err=>{
      console.log(err)
    })

2、第一个.then()中没有直接的 return 一个Promise,代码会顺序执行第二个.then()

3、第一个.then()中如果有直接的 return 一个Promise(流水线上return了一个Promise),代码才会 等待 return 的这个Promise,有了 resolve 或 reject 状态后, 再执行第二个.then()

 

4、如果Promise内部出错,并且内部reject了错误(如果没有reject错误,会直接报错),则这个Promise会寻找距离他最近的.catch()方法执行

   如果Promise内部没有出错,则这个Promise会寻找距离他最近的.then()方法执行

   如果app.isLogin()内部出错,并且内部reject了错误(如果没有reject错误,会直接报错),会执行距离他最近的(也就是最下边那个).catch()方法

   如果app.isLogin()内部没有出错,会执行距离他最近的(紧挨着他的那个).then()方法

onLookPhone(){
    app.isLogin()
    .then(res=>{
      return app.clearTempScore()
    })
    .then(res=>{
      return app.getUserScore()
    })
    .then(res=>{
      console.log('.then3')
    })
    .catch(err=>{
      console.log(err)
    })
  },
posted @ 2019-12-15 13:21  254980080  阅读(3545)  评论(0编辑  收藏  举报