簡體   English   中英

然后回調函數中的違約承諾鏈

[英]broken promise chaining in then callback function

如何在此處終止諾言:

    .then(resp => {
      if (xxxxx) {
        return nextPromise()
      } else {
        // stop promise chain
        return
      }
    })
    .then(resp => {
       // nextPromise callback function..
    })

我以為退貨會停止連鎖經營,但我錯了。

如果您不想通過拋出錯誤來破壞鏈條,則解決方案是嵌套鏈條:

.then(resp => {
  if (xxxxx) {
    return nextPromise().then(resp => {
      // nextPromise callback function..
    });
  }
})

這仍然允許使用全局.catch() ,而您不必在其中顯式檢查拋出的錯誤以結束鏈。 缺點是,如果您有許多這樣的條件,最終會得到類似於“ callback hell”的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM