關於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  
      
複製代碼
相關文章
相關標籤/搜索