I m actually coding some functions using chained promises and I m having some trouble dealing with them.
Actually, here's the anti-pattern that I want to avoid :
return promise.then((res)=>{
return promise2.then((result)=>{
return result;
},(error)=>{
return Promise.reject(error);
});
},(err)=>{
return Promise.reject(err);
});
I tried refactoring it the following :
return promise.then((res)=>{
return promise2;
},(err)=>{
return Promise.reject(err);
}).then((result)=>{
return result;
},(error)=>{
return Promise.reject(error);
});
The fact is that I don't know at all of my rejected promises will act. It seems that my unit tests are all breaking after this refactor and I don't understand why. Maybe the behaviour...
In fact, does my (error) callback contain the (err) rejected value, or will it break completely and return the promise directly ?
Thanks for your help.
EDIT : okay I ll try with another example . Let's imagine that I need to make some arithmetic opeartions asynchronously by catching each possible error, at each possible level
asyncAddition(5, -5).then((res)=>{
return asyncDivide(100, res);
},(err)=>{
return Promise.reject('ERROR, you cant divide by 0');
}).then((res)=>{
return asyncMultiply(res, 10);
}).then((res)=>{
return res;
},(err)=>{
return Promise.reject('An error occured, number result is over 1000 ');
});
Does the first promise rejected will break all the other in the chain ? Or will it continue ?