2

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!

4

2 に答える 2

4

作業が完了したときに関数が呼び出すコールバック パラメーターを関数がサポートしている場合forEachSerialは、ライブラリのメソッドを使用してこれをかなりきれいに行うことができます。asyncdoThis

このようなもの:

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
    });
});
于 2012-10-19T00:30:12.527 に答える
0

resultイベントはデータの各行で発生します。ドキュメントを参照してください

connection.query('select * from data')
  .on('result', function(row) {
     doThis(row.axn, row.dnq, row.bgq);
  });
于 2012-10-19T01:04:32.860 に答える