title: Calling async function multiple times
body: <p>So I have a method, which I want to call multiple times in a loop. This is the function:</p>
  
  <pre><code>function PageSpeedCall(callback) {
      var pagespeedCall = `https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=https://${websites[0]}&amp;strategy=mobile&amp;key=${keys.pageSpeed}`;
      // second call
      var results = '';
      https.get(pagespeedCall, resource =&gt; {
          resource.setEncoding('utf8');
          resource.on('data', data =&gt; {
              results += data;
          });
          resource.on('end', () =&gt; {
              callback(null, results);
          });
          resource.on('error', err =&gt; {
              callback(err);
          });
      });
      // callback(null, );
  }
  </code></pre>
  
  <p>As you see this is an async function that calls the PageSpeed API. It then gets the response thanks to the <code>callback</code> and renders it in the view. Now how do I get this to be work in a for/while loop? For example</p>
  
  <pre><code>function PageSpeedCall(websites, i, callback) {
      var pagespeedCall = `https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=https://${websites[i]}&amp;strategy=mobile&amp;key=${keys.pageSpeed}`;
      // second call
      var results = '';
      https.get(pagespeedCall, resource =&gt; {
          resource.setEncoding('utf8');
          resource.on('data', data =&gt; {
              results += data;
          });
          resource.on('end', () =&gt; {
              callback(null, results);
          });
          resource.on('error', err =&gt; {
              callback(err);
          });
      });
      // callback(null, );
  }
  
  var websites = ['google.com','facebook.com','stackoverflow.com'];
  for (let i = 0; i &lt; websites.length; i++) {
      PageSpeedCall(websites, i);
  }
  </code></pre>
  
  <p>I want to get a raport for each of these sites. The length of the array will change depending on what the user does.</p>
  
  <p>I am using <code>async.parallel</code> to call the functions like this:</p>
  
  <pre><code>let freeReportCalls = [PageSpeedCall, MozCall, AlexaCall];
  
  async.parallel(freeReportCalls, (err, results) =&gt; {
      if (err) {
          console.log(err);
      } else {
          res.render('reports/report', {
              title: 'Report',
              // bw: JSON.parse(results[0]),
              ps: JSON.parse(results[0]),
              moz: JSON.parse(results[1]),
              // pst: results[0],
              // mozt: results[1],
              // bw: results[1],
              al: JSON.parse(results[2]),
              user: req.user,
          });
      }
  });
  </code></pre>
  
  <p>I tried to use promise chaining, but for some reason I cannot put it together in my head. This is my attempt.</p>
  
  <pre><code>return Promise.all([PageSpeedCall,MozCall,AlexaCall]).then(([ps,mz,al]) =&gt; {
      if (awaiting != null)
          var areAwaiting = true;
      res.render('admin/', {
          title: 'Report',
          // bw: JSON.parse(results[0]),
          ps: JSON.parse(results[0]),
          moz: JSON.parse(results[1]),
          // pst: results[0],
          // mozt: results[1],
          // bw: results[1],
          al: JSON.parse(results[2]),
          user: req.user,
      });
  }).catch(e =&gt; {
      console.error(e)
  });
  </code></pre>
  
  <p>I tried doing this: </p>
  
  <pre><code>return Promise.all([for(let i = 0;i &lt; websites.length;i++){PageSpeedCall(websites, i)}, MozCall, AlexaCall]).
  then(([ps, mz, al]) =&gt; {
      if (awaiting != null)
          var areAwaiting = true;
      res.render('admin/', {
          title: 'Report',
          // bw: JSON.parse(results[0]),
          ps: JSON.parse(results[0]),
          moz: JSON.parse(results[1]),
          // pst: results[0],
          // mozt: results[1],
          // bw: results[1],
          al: JSON.parse(results[2]),
          user: req.user,
      });
  }).catch(e =&gt; {
      console.error(e)
  });
  </code></pre>
  
  <p>But node just said it's stupid.</p>
  
  <p>And this would work if I didn't want to pass the websites and the iterator into the functions. Any idea how to solve this?</p>
  
  <p>To recap. So far the functions work for single websites. I'd like them to work for an array of websites.</p>
  
  <p>I'm basically not sure how to call them, and how to return the responses.</p>