title: Conditional promise chain
body: <p>I'm looking to write some code that chains together some promises. I have a condition that based on the result of one promise I either call the next function that returns a promise and continue chaining another few functions, or I do nothing (effectively end the promise chain).</p>
  
  <p>I have the following three possible solutions, I kinda think they all are a bit messy though.</p>
  
  <p>Here is my first approach, what I dislike here is the nested promises.</p>
  
  <pre><code>initalAsyncCall()
    .then((shouldContinue) =&gt; {
      if (shouldContinue) {
        return nextStep()
          .then(() =&gt; anotherStep())
      }
    })
    .catch((error) =&gt; {
      handleError(error);
    })
  </code></pre>
  
  <p>Here is my second. This one seems a bit longer and possibly harder to read</p>
  
  <pre><code>const shouldContinuePromise = initialAsyncCall();
  const nextStepPromise = shouldContinuePromise.then((shouldContinue) =&gt; {
    if (shouldContinue) return nextStep();
  });
  Promise.all([shouldContinuePromise, nextStepPromise])
    .spread((shouldContinue) =&gt; {
      if (shouldContinue) return anotherStep();
    })
    .catch((error) =&gt; {
      handleError(error);
    });
  </code></pre>
  
  <p>And finally here is my last approach. What I don't like here is I am throwing an error when it's not really an error.</p>
  
  <pre><code>initalAsyncCall()
    .then((shouldContinue) =&gt; {
      if (!shouldContinue) throw new HaltException()
      return nextStep();
    })
    .then(() =&gt; anotherStep())
    .catch(HaltException, (ex) =&gt; {
      // do nothing... maybe some logging
    })
    .catch((error) =&gt; {
      handleError(error);
    })
  </code></pre>