title: How to make loop wait for callback in node.js?
body: <p>I have scenario. Where i Want to execute loop after data has been updated in mongodb. Means Like that : </p>
  
  <pre><code>var i = 0;
  while (i&lt; 5) {
      attendanceDataModel.update(query, condition).exec(function(error, data) {
          if (error) {
              console.log("Error @ 168 line in app.js File : \n" + err + "\n");
              i++;
          } else {
              if (data.length &lt;= 0) {
                  console.log("No Records Matched.");
                  i++;
              } else {
                  console.log(currEmpId + " : successfully Logged Out ! :-)" + data + "\n");
                  updateRecordNumber(currRecordNumber); 
                  i++;  //wrong because it increases the value before updating in DB.
              }
          }
      });
  }
  
  var updateRecordNumber = function(currRecordNumber) {
  
      var condition = { deviceLogId: parseInt(currRecordNumber) };
  
      lastDeviceLogIdModel.update({}, condition).exec(function(error, data) {
          if (error) {
              console.log("Error @ 213 line in app.js File : \n" + err + "\n");
          } else {
              if (data.length &lt;= 0) {
                  console.log("No Records Matched." + "\n");
              } else {
                  console.log(currRecordNumber + " : DeviceLogId successfully Updated ! :-)");
        // I want to increase value of i here after updation in database
              }
          }
      });
  }
  </code></pre>
  
  <p><strong>Now, I want to increase variable i value after function updateRecordNumber has successfully updated</strong></p>