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

Understanding JavaScript Closures in For Loops

How do you pass a variable into a function inside a loop?
It always ends up taking the last value!
What gives?

Let’s suppose you’re lucky enough to work for a company that gives annual cost-of-living pay increases. Let’s also suppose the corporate payroll system is written in JavaScript. (Why not? everything seems to be JavaScript these days)

And, because we’re big enterprise, our system is rife with inefficiency and we’ve over-engineered everything. Below is what the payroll function looks like. (This is a totally contrived example… so you’ll have to practice suspension of disbelief rather than ask why it’s designed so poorly)

// Queue jobs to assign employee pay increases
function queueEmployeeRaises(employees, jobQueue, database, multiplier) {
  for (var i = 0; i < employees.length; i++) {
    var employee = employees[i];
    jobQueue.push(function() {
      if (employee.title == 'CEO') {
        employee.salary = employee.salary * Math.max(1.20, multiplier);
      }
      else {
        employee.salary = employee.salary * Math.min(multiplier, 1.02);
      }
      database.save(employee);
    });
  }
}

Continue reading “Understanding JavaScript Closures in For Loops”

Understanding JavaScript Closures in For Loops

Redux Hero Part 4: Every Hero Needs a Villain (A Fun Introduction to redux-saga.js)

Dragon Warrior: Blue Slime Draws Near

When you think about a classic RPG like Dragon Warrior or Final Fantasy, the bulk of the game involves wandering around on a map and getting into fights with monsters. Does the hero encounter a monster on every tile? No. Randomness is the key.

But where does randomness fit in a Redux world where everything is supposed to be a pure function? Deterministic and without side effect.

And there’s a larger question of Where does my application logic belong in general? It’s difficult to reason about an application when the logic is scattered across multiple reducers, each of which has a signal-to-noise ratio that is a lot lower (by design!) than other average code you write.

Fortunately, redux-saga provides a nice solution to these problems. We’re going to build up some logic for our mock game that demonstrates this.

Continue reading “Redux Hero Part 4: Every Hero Needs a Villain (A Fun Introduction to redux-saga.js)”

Redux Hero Part 4: Every Hero Needs a Villain (A Fun Introduction to redux-saga.js)