I work in a nodejs project and want to skip the promise in the chain. Below is my code. In the first block of the promise, it will solve the value {success: true}
. In the second block, I want to check the success
value, if true, I want to return the value to the called one and skip the rest of the promises in this chain; continue the chain if the value is false. I know that I can throw an error or reject it in the second block, but I need to handle the case of an error that is not an error. So how can I achieve this in the promise chain? I need a solution without any other third party library.
new Promise((resolve, reject)=>{ resolve({success:true}); }).then((value)=>{ console.log('second block:', value); if(value.success){ //skip the rest of promise in this chain and return the value to caller return value; }else{ //do something else and continue next promise } }).then((value)=>{ console.log('3rd block:', value); });
source share