Synchronously chain BlueBird promises together within an array
<p>I'm trying to have a series of promises executed synchronously, chaining them together, but only having certain promises added based on a condition..</p>

<p>Heres an example of what I mean:</p>

<pre class="lang-js prettyprint-override"><code>const Promise = require('bluebird')

const funcA = int =&gt; new Promise( res =&gt; res(++int) )
const funcB = int =&gt; new Promise( res =&gt; res(++int) )
const funcC = int =&gt; new Promise( res =&gt; res(++int) )

let mainPromise = funcA(1)

// Only execute the funcB promise if a condition is true
if( true )
    mainPromise = mainPromise.then(funcB)

mainPromise = mainPromise.then(funcC)

mainPromise
    .then( result =&gt;  console.log('RESULT:',result))
    .catch( err =&gt; console.log('ERROR:',err))
</code></pre>

<p>If the boolean is true, then the output is: <code>RESULT: 4</code>, if its false, then its <code>RESULT: 3</code>, which is exactly what I'm trying to accomplish.</p>

<p>I figured there should be a better, cleaner way to do this though. I'm using the Bluebird promise library, which is pretty powerful. I tried using <code>Promise.join</code>, which didn't yield the desired result, and neither did <code>Promise.reduce</code> (But I may have been doing that one incorrectly)</p>

<p>Thanks</p>