Wait until foreach finishes

Currently I am using node-mysql (https://github.com/felixge/node-mysql) and for each result it returns from the database I have to perform several functions. Currently, it attempts to complete everything at once, however is there anyway to make it wait until each function is complete before moving to the next result from mysql?

connection.query('select * from data', function(err, rows) {
for (var i = 0; i < rows.length; i++) {
     doThis(rows[i].axn, rows[i].dnq, rows[i].bgq);
 }
});

In the doThis function, if certain requirements are met than it passes the data to another function. How can I make sure that those are completed first before moving until the next query row?

Thank you in advance!

You can do this pretty cleanly with the forEachSerial method of the async library if you have your doThis function support a callback parameter that it calls when its work is done.

Something like this:

connection.query('select * from data', function(err, rows) {
    async.forEachSeries(rows, function(row, callback) {
        doThis(row.axn, row.dnq, row.bgq, callback);
    }, function(err) {
        // All done
    });
});

result event is fired on each row of data, see documentation

connection.query('select * from data')
  .on('result', function(row) {
     doThis(row.axn, row.dnq, row.bgq);
  });