关于promise的一些总结

promise

node、JavaScript、解决异步回调javascript

用法:java

  • 在promise中 若是有多个then函数,下一个then中的第一个(参数)函数中的参数将会是 上一个then 的第一个(参数)函数的return 结果
const promise = new Promise(function(resolve, reject) {
    reject('这里走的是catch')  // 这里会报错 但不会影响呈现执行 缘由: reject中应当传入一个Error对象 例如:reject(new Error('errorMsg'))
    // resolve('请求成功')
    })
    promise.then(function(result) {
    console.log(result) // -- 请求成功
    return '下一个then的result'
    }).then(function(result) {
    console.log(result) // -- 下一个then的result
    }).catch(function(error) {
    console.log(error)  // -- 这里走的是catch
    })

复制代码
  • 在一个then中throw 一个错误以后,后面的then将会跳过直接执行 catch
  • 在catch后面若是有then 将会在catch执行后继续执行,且第一个参数是 catch中return的值
  • 若是没有走catch 而catch后面还有then 也将继续执行但catch后的第一个then的参数将为undefined
const promise = new Promise(function(resolve, reject) {
        resolve('请求成功')
      })
      promise.then(function(result) {
        console.log(result) // -- 请求成功
        throw new Error('出差了')
      }).then(function(result) {
         console.log('前一个then throw 一个错误 这里将被跳过')
      }).catch(function(error) {
        console.log(error)  // -- 出差了
        return '出错后执行'
      }).then(function(res) {
        console.log(res) // -- 出错后执行
      })
复制代码
  • Promise.resolve() 和 Promise.reject() 是手动建立一个已经 resolve 或者 reject 的 Promise 快捷方法。
  • Promise.all() 和 Promise.race() 是并行运行异步操做的两个组合式工具。既咱们能够发起并行操做,而后等多个操做所有结束后进行下一步操做
// Promise.all的参数是个数组,callBack的参数也是数组对应all的返回
Promise.all([func1(), func2(), func3()])
.then(([result1, result2, result3]) => { /* use result1, result2 and result3 */ });
// Promise.race()的参数是个数组,callBack的参数为最早执行完的函数返回值
Promise.race([p1, p2]).then((result) => {
  console.log(result)  // p1 和 p2 那个快(先执行完并返回结果)返回那个
}).catch((error) => {
  console.log(error)  // 打开的是 'failed'
})
复制代码

常见问题

  • 在then中若是 嵌套有其余异步操做 若是 直接return 这个异步 则会等等 不然 直接执行下一个then
  • 这里尝尝会致使代码的执行效果与想象中的不同
const promiseInit = new Promise(function(resolve,reject) {
          setTimeout(function() {
              resolve('这个是setTimeout的输出')
          }, 1000)
      })

      const promise = new Promise(function(resolve, reject) {
        // reject('这里走的是catch') // 这里会报错 但不会影响呈现执行 缘由: reject中应当传入一个Error对象 例如:reject(new Error('errorMsg'))
        resolve('请求成功')
      })
      promise.then(function(result) {
        console.log(1000, result) // -- 请求成功
        // throw new Error('出差了')
      }).then(async function(result) {
        return promiseInit.then(function(aaa) {
            console.log(111, aaa)
            return aaa
         })
       // return 'name'
      }).then(function(res) {
        console.log(222, res)
      })
    以上执行顺序为 
    -- 1000
    -- 111
    -- 222
    
    const promise = new Promise(function(resolve, reject) {
        // reject('这里走的是catch') // 这里会报错 但不会影响呈现执行 缘由: reject中应当传入一个Error对象 例如:reject(new Error('errorMsg'))
        resolve('请求成功')
      })
      promise.then(function(result) {
        console.log(1000, result) // -- 请求成功
        // throw new Error('出差了')
      }).then(async function(result) {
        promiseInit.then(function(aaa) {
            console.log(111, aaa)
            return aaa
         })
       return 'name'
      }).then(function(res) {
        console.log(222, res)
      })
    以上执行顺序为 
    -- 1000
    -- 222
    -- 111  
      
复制代码
相关文章
相关标签/搜索