zoukankan      html  css  js  c++  java
  • 多个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)
        })
      },
  • 相关阅读:
    poj2502(最短路)
    poj1511(最小环和)
    uva11090(spfa判负环)
    hdu4370(spfa最短路最小环)
    uva10561(SG)
    uvalive5059(SG)
    uvaliva3905(扫描线)
    scu4445(模拟)
    uvalive3902(树上的最优化 贪心)
    scu4444(完全图最短路)
  • 原文地址:https://www.cnblogs.com/qq254980080/p/12043856.html
Copyright © 2011-2022 走看看