Promiseチェーンを中断する方法


Promiseチェーンを中断する方法
  • Promiseインスタンス
  • を作成
    const promise = new Promise(function(resolve, reject) {
      // ... some code
    
      if (/*        */){
        resolve(value);
      } else {
        reject(error);
      }
    });
    
  • thenメソッド
  • を使用
    promise
    .then(res=>{
    	// success
    })
    .catch(err=>{
    	// failure
    })
    
    
  • Promiseチェーンで中断する場合.thenメソッド
  • 方法1 throw err
    promise
    .then(res=>{
    	if(!res){
    		throw err //     
    	}
    })
    
  • 方法2 reject
  • promise
    	.then(res=>{
    		if(!res){
    			reject //   reject  
    		}
    	})
    
  • Promise関連サードパーティライブラリpromise-funこのようなthen()でpromiseチェーンを中断する実装は、中のp-if:Conditional promise chains
  • を使用することができる.