I would like to execute the asynch function conditionally, but I think that I am missing the correct syntax to say what I want.
this.doUpToThreeThings = function(skipTheMiddleStep) {
return doFirstThing().then(function (result) {
if (skipTheMiddleStep) {
return doThirdThing();
} else {
return doSecondThing();
}
}).then(function (result) {
if (skipTheMiddleStep) {
return "ok";
} else {
return doThirdThing();
}
});
}
By the time we move to the second, I donβt know if the first block has taken the middle step, so I have to repeat the condition. And the second block reads strangely: he must say that if you skip the middle step, then do the third thing, but since we know that the previous block should have done the third, it just comes back. So, I have to repeat the condition and write quite the wrong code in the second.
I understand that I can write a function called doSecondAndThirdThings and just call it from the condition in the first block, but it's not very DRY, it just hides non-randomness. (or maybe I'm wrong?)
, "ok". , - ? -