title: Async call with iteration in Node.JS
body: <p>I'm doing a practice problem to familiarize myself with Node. I set up my app, routes, and have a controller as well.</p>
  
  <p>I use an outside API and pass in different ids, and (later) I want to compare the response to a given value passed to the api I'm writing. So I am iterating over an array of ids and passing them to the api call (via the path in options) to collect all the responses. </p>
  
  <p>I keep getting confused by older examples and docs... for a while I was attempting to do it with Promises until I read about async. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>/**
   * Controller
   */
  
  const url = 'api.outsidesite.com';
  const http = require('http');
  productids = [
    1, 2, 3, 4, 5, 6, 7, 8, 9
  ];
  
  const options = {
    hostname: url,
    path: '/v1/items/${productid}?format=json&amp;apiKey=${apiKey}',
    method: 'GET',
    timeout: 10000
  };
  
  
  exports.findOne = (request, response) =&gt; {
    const req = http.request(options, res =&gt; {
      res.on('data', chunk =&gt; {
        response.send(chunk);
      });
    });
    req.on('error', e =&gt; {
      console.error(e);
    });
    req.end();
  };</code></pre>
  </div>
  </div>
  </p>
  
  <p>in my router I'm calling findOne this way:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  app.get('api/products/findone/:value', products.findOne);</code></pre>
  </div>
  </div>
  </p>
  
  <p>Now I'm stuck on iterating over and inserting the ids. I've been reading up on async/await and doing the calls in series.</p>
  
  <p>Was thinking about using the built in <a href="https://www.npmjs.com/package/request" rel="nofollow noreferrer">Request</a> library but I don't know if it's really necessary. </p>
  
  <p>I tried to do it based off of this <a href="https://stackoverflow.com/a/24985483">stackoverflow answer on iterating over promises</a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code> const fetchProductDetails = productids =&gt; {
     return productids.reduce(function(promise, product) {
       return promise.then(function() {
         return findOne.done(function(res) {
           console.log(res);
         });
       });
     }, Promise.resolve());
   };
  
   fetchProductDetails(productids).then(function() {
     console.log('all done');
   });</code></pre>
  </div>
  </div>
  </p>
  
  <p>but then I realized that the answer was 4 years old and there's probably a better way of doing it now. Plus the call to findOne was undefined so I got a bit confused about how to reference the export.findOne function with express in the mix. Any ideas on a clean solution for iterating?
  Considering using <a href="https://caolan.github.io/async/docs.html#series" rel="nofollow noreferrer">async series</a> or something like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>getAll = () =&gt;
    async.reduce(
  productids,
  0,
  function(memo, item, callback) {
    // pointless async:
    process.nextTick(products.findOne());
  },
  function(err, result) {
    console.log(err);
  }
    );</code></pre>
  </div>
  </div>
  </p>
  
  <p>but I keep implementing older solutions and don't want to go down a wrong path again if there is a simple way to do this. Thanks!</p>
  
  <p>Resources:</p>
  
  <p><a href="https://nemethgergely.com/async-function-best-practices/" rel="nofollow noreferrer">async best practices</a></p>
  
  <p><a href="https://louislam.net/blog/2018/03/node-js-callback-vs-promise-vs-asyncawait/" rel="nofollow noreferrer">callback vs promise vs asyncwait</a></p>