Promise waterfall
<p>I'm an API developer and typically write end-points requiring passing a result from one async call to another async call aka async waterfall.</p>

<p>I usually do this the following way using promises:</p>

<pre><code>task1()

.then(result1){

    task2(result1)

    .then(result2){

        task3(result2)

        .then(result3){
            // API response
        })

        .catch(function(err){
            // Task 3 handle err
        })
    })

    .catch(function(err){
        // Task 2 handle err
    })
})

.catch(function(err){
    // Task 1 handle err
})
</code></pre>

<p>It's obvious that not much has been gained over using callbacks. Instead of "callback hell" I now got "promise hell".</p>

<p>I have looked at <a href="https://www.npmjs.com/package/bluebird" rel="noreferrer">npm bluebird</a> but it does not seem that waterfall promises are supported.</p>

<p>Sometimes I would use <a href="https://www.npmjs.com/package/async" rel="noreferrer">async</a> and wrap tasks that return a promise:</p>

<pre><code>const tasks = [

    job1: function(cb){

        task1()

        .then(function(result){
            cb(null, result);
        })

        .catch(function(err){
            cb(err);
        })  
    },

    job2: function(cb, result1){

        task2(result1)

        .then(function(result){
            cb(null, result);
        })

        .catch(function(err){
            cb(err);
        })  
    },

    job3: function(cb, result2){

        task3(result2)

        .then(function(result){
            cb(null, result);
        })

        .catch(function(err){
            cb(err);
        })  
    }
]

async.series(tasks, function(err, results){

    if(err){
        // handle error
    }

    // API callback
});
</code></pre>

<p>But this is also pretty useless.
And if you're thinking about <code>Promise.all</code> that wont work since the result from one tasks is not passed on to the next.</p>

<p>What's the better approach?</p>