title: Call function when for loop finished in Node js
body: <pre><code>function inboxUsers(){
      for (var i=0; i&lt;uniqueArray.length; i++){
          var getUsername    = 'SELECT userName FROM users WHERE userId = ' + uniqueArray[i];
          db.query(getUsername, function(err, results) {
              if (err) {
                  console.log('Error in database');
                  throw err;
              }
              for(var i in results){
                  console.log('single',results[i].userName);
                  inboxUserList.push(results[i].userName);
              }
          });
      }
      sample();
  }
  
  function sample(){
      console.log('same function');
  }
  </code></pre>
  
  <p>This is my console output.</p>
  
  <pre><code>same function
  single user1
  single user2
  single user3
  </code></pre>
  
  <p>In this code I called the function <strong>sample()</strong> after for loop, but it called the <strong>sample() function</strong> before for loop ends. </p>
  
  <p>I want to call the sample() function when for loop ends. I am a beginner for stackoverflow, if i have error please apologize me. Thank you</p>