Back to Basics: Running Promises in Serial with Array.reduce()

Running JavaScript Promises in parallel is as simple as calling Promise.all() with an array of Promises. But sometimes you need to run then in sequential order. What then?

// Parallel
return Promise.all([
    task1,
    task2,
    task3,
]).then(arrayOfResults => {
    // Do something with all results
});

We could trivially chain these promises together in serial fashion with .then()

// Serial
return task1.then(task2).then(task3);

However this is only feasible when you can hardcode tasks. It also prevents you from capturing the return values from task1 and task2.

Continue reading “Back to Basics: Running Promises in Serial with Array.reduce()”

Back to Basics: Running Promises in Serial with Array.reduce()