title: How to Run an API Calls in Parallel (Node.js)
body: <p>I am trying to run some API calls in parallel, but am having problems since I am trying to call a function again before the API data has been returned.</p>
  
  <p>I am thinking that I could possibly use the <code>new</code> command in Node, but am not sure how to structure it into this scheme. I am trying to avoid recursion, as I already have a recursive version working and it is slow.</p>
  
  <p>Currently I am trying to this code on the server.</p>
  
  <pre><code>loopThroughArray(req, res) { 
    for(let i=0; i&lt;req.map.length; i++) {
      stack[i] = (callback) =&gt; {
        let data = getApi(req, res, req.map[i], callback)
      }
    }
  
    async.parallel(stack, (result) =&gt; {
        res.json(result)
    })
  }
  </code></pre>
  
  <p>....</p>
  
  <pre><code>function getApi(req, res, num, cb) {
    request({
      url: 'https://example.com/api/' + num
    },
    (error, response, body) =&gt; {
      if(error) {
        // Log error
      } else {
        let i = {
          name: JSON.parse(body)['name'],
          age: '100'
        }
        console.log(body) // Returns empty value array.length &gt; 1 (req.map[i])
        cb(i)
      }
    })
  </code></pre>
  
  <p>Is there a way to spawn new instances of the function each time it's called and accumulate the results to send back as one result to the client?</p>