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()

Say Goodbye to ‘../../../..’ in your TypeScript Imports

Don’t you hate writing import lines and not being sure how many dot-dot-slashes you need to get to the right place? Sure, you can look over at the project tree but there are so many files that you’ve got to scroll and scroll.

Oh no… Was that '../../../../' or '../../../../../'? [scrolls back down]. It drives me bananas.

Things are even worse if you restructure your project tree and some files move higher or lower. Or you copy-paste import sections between files that are at different levels in the project tree. Now your watch window is a sea of red.

But… it shouldn’t matter if your dependency is up four directories vs. five. Unfortunately, that’s just how module resolution works in Node.js.

Wouldn’t it be great if you could forget about relative paths entirely?

You could have the deepest, most complex project structure… bring it on.

Continue reading “Say Goodbye to ‘../../../..’ in your TypeScript Imports”

Say Goodbye to ‘../../../..’ in your TypeScript Imports

What is the right way to do asynchronous operations in Redux?

It’s understandable why so many newcomers to React+Redux have difficulty wrapping their heads around asynchronous actions. React is simply a view layer. Redux is simply a state management layer. But it takes much more to build a typical app.

These poor developers are given the corner pieces of a puzzle and the rest of the pieces are scattered about on the ground. There’s no reference picture to see what the puzzle is supposed to look like — everyone you ask would show you a different picture anyways.

Even developers experienced in React+Flux have difficulty picking up Redux because now they’re told not to put asynchronous calls in their components. Well then, where?

Continue reading “What is the right way to do asynchronous operations in Redux?”

What is the right way to do asynchronous operations in Redux?