title: Nodejs: Issue with async and forEach - Need to wait the async to resolve
body: <p>I'm fairly new with node, and I've been learning and dealing with async/promises. Right now I'm trying to create a process which makes an insert from a DB (10K rows for example), calls a webservice that transforms one column and then make an insert for the modified data.</p>
  
  <p>So, I make an Oracle SQL query, and for the result, I do a foreach:</p>
  
  <pre><code>let counter = 0;
  var binds = [];
  
  res.rows.forEach((row) =&gt; {
  
      var original_data = row[0];
  
      Transform(original_data).then(new_data =&gt; {
  
          counter++;
  
          binds.push([original_data,new_data]);
  
          if (counter % 1000 === 0){
              console.log(`1K rows`);
              doInserts(binds);
              binds = [];
          }
  
      });
  
  });
  </code></pre>
  
  <p>I'm calling the <code>doInserts</code> every 1000 rows, so I don't have many transactions open on Oracle.</p>
  
  <p>The Transform function calls a webservice, which resolves with the value that I need.</p>
  
  <pre><code>function Transform(value){
  
      return new Promise(function(resolve, reject){
          var requestPath = `http://localhost:3000/transform/${value}`;
          var req = request.get(requestPath, function(err, response, body){
              if (!err &amp;&amp; response.statusCode == 200){
                  resolve(body);
              }else{
                  reject("API didn't respond.");
              }
          }).end();
      });
  
  }
  </code></pre>
  
  <p>However, this is clogging the webservice (I use the <a href="https://github.com/request/request" rel="nofollow noreferrer">request</a> library to connect) when the foreach has 10K rows. I am thinking, the foreach is not doing the Transform one at a time synchronously.</p>
  
  <p>This is probably me not knowing lots of node, async, promises.. but I'm puzzled. Can anyone help?</p>