title: How do I chain a set of functions together using promises and q in node.js?
body: <p>I have some dynamic data that needs to have work performed on it. The work must happen sequentially. Using the <a href="https://github.com/kriskowal/q" rel="nofollow noreferrer">Q Library</a>, I'd like to create an array of functions and execute the code sequentially using sequences. I can't seem to quite figure out the syntax to achieve this.</p>
  
  <pre><code>const fruits = ["apple", "cherry", "blueberry"]
  
  function makeFruitPie (fruit) {
    return Q.Promise((resolve, reject) =&gt; {
      // Do some stuff here
      resolve(fruit+" pie")
  
      // Error handling here
      reject(new Error(""))
    })
  }
  
  const fruitFuncs = new Array(fruits.length)
  
  for(var i = 0; i &lt; fruits.length; i++) {
    fruitFuncs[i] = makeFruitPie(fruits[i])
  }
  
  // Stole this example from the github docs but can't quite get it right.
  i = 0
  var result = Q(fruits[i++])
  fruitFuncs.forEach((f) =&gt; {
    result = result(fruits[i++]).then(f)
  })
  </code></pre>