How do I break a promise chain?

前端 未结 4 1496
北恋
北恋 2020-12-03 12:19

How should I stop the promise chain in this case? Execute the code of second then only when the condition in the first then is true.

var p = new Promise((res         


        
4条回答
  •  没有蜡笔的小新
    2020-12-03 12:51

    You could move the chain into the conditional branch:

    p.then((res) => {
      if(true) {
        return Promise.resolve(res + 2).then((res) => {
          // executed only when the condition is true
        });
      } else {
        // do something 
        // chain ends here
      }
    });
    

提交回复
热议问题