title: Node JS Api request in loop
body: <p>I'm trying my damndest to avoid callback hell with my Node JS. But I'm trying to make a large number of api-requests and insert these into my database.</p>
  
  <p>My issue here (of course) is that my for-loop runs and increments i before I finish my request and database insertion.</p>
  
  <pre><code>for(var i = 0; i &lt;= 1 ; i++){
      apiRequest = data[i]; 
      apicall(apiRequest);
  }
  
  
  function apicall(urlApi){
      request((urlApi), function(error, response, body){
          if(error){
              console.log("error");
          } else if(!error &amp;&amp; response.statusCode == 200){
              var myobj = JSON.parse(body);
              dbInsert(myobj);
          }
      });
  }
  
  function dbInsert(obj) {
      //insert into database
  }
  </code></pre>
  
  <p>If someone else would come by this question I can truly recommend this blogpost which I found after reading the response by <strong>joshvermaire</strong>:</p>
  
  <p><a href="http://www.sebastianseilund.com/nodejs-async-in-practice" rel="nofollow noreferrer">http://www.sebastianseilund.com/nodejs-async-in-practice</a></p>