3 ms·
When I can't return early, I find that the && operator, because of its short-circuit evaluation, is extremely useful for writing chains of "everything before mu
by userbinator 8y ago
When I can't return early, I find that the && operator, because of its short-circuit evaluation, is extremely useful for writing chains of "everything before must successfully execute" without the labourious series of "if/else ladders" or deep nesting that would otherwise be needed. Compare:
success = doOneThing()
&& doSecondThing()
&& doThirdThing()
...
&& doLastThing();
and the more common "traditional" way:
success = doOneThing();
if(success)
{
success = doSecondThing();
}
if(success)
{
success = doThirdThing();
}
...
if(success)
{
success = doLastThing();
}
- 8note 8y agopromise chains do that pretty nicely Promise.start() .then(doOnething) .then(doSecondThing) .then(doThirdThing) .then(doLastThing) .catch(handlesomeerrors)
- coldtea 8y agoThat's because promises kind of Monad-ic.
- s4vi0r 8y agoWhat you want is a monad. Instead of loading up every language with stupid keywords that are meant to simplify monadic code (but only for one domain), language designers should really take a page out of Haskell or Scala's book and think seriously about unifying on a do notation/for comprehension type construct. That way regardless of if you're working with promises or not, you don't have to worry about hacking it with && or nesting 10 layers deep with if/else.