Processing a list of pairs of items using promises and functional programming
Does this function break the paradigm of functional programming in JS?


Short answer: It depends

One common response I get from Software Engineering SE on this topic is you don't have to be pure all the time. A function can still act functional even if the implementation isn't written in a functional manner. Take for example the following:

// All return an array with numbers starting from s to e with no
// side-effects and takes all input from args. The first one is
// obviously not "functional" but works just as well as the others.

function range(s, e){
  const array = []
  for(let i = s; i &lt; e; i++){
    array.push(s)
  }
  return array
}

function range(s, e){
  return Array(e - l).fill(null).map((v, i) =&gt; s + i)
}

function range(s, e){
  return s == e ? [] : [s, ...range(s + 1, e)]
}


Striking a balance between ideal and practical is also a deciding factor. For instance, recursion is not a foreign concept in JS. But historically, due to stack size limits, loops became more prevalent. You can safely assume people can read loops better than recursion, and are therefore more likely to understand the first sample than the other two.



It looks like the purpose of process function is to batch-process arrays of items. This can be done with recursion. The idea is to process the current item, then concat the results of the next call, which does the same thing until there's nothing in the array. If you use Node or have Babel to transpile, you can use async/await to simplify the syntax.

const process = async (i) =&gt; {
  if (!i.length) return []

  const r1 = await Promise.all(i[0].map(action))
  const r2 = await process(i.slice(1))

  return [...r1, ...r2]
}


If you can't do async/await, here's an expanded version with regular promise syntax.

function process(i) {
  if (!i.length) return Promise.resolve([]) 

  return Promise.all(i[0].map(action)).then(function(r1) {
    return process(i.slice(1)).then(r2 =&gt; r1.concat(r2))
  })
}