6

How to show 'Finish' after screening all row.name results ?

npmSql.query("SELECT name FROM sqlTable", function(err, rows, fields) {
  rows.forEach(function(row) {
    console.log(row.name);
  });
}, function () { console.log('Finish'); });

I was trying to follow some answers like Node.js - Using the async lib - async.foreach with object but had no luck :(

Update

function abc() {
    npmSql.query("SELECT name FROM sqlTable", function(err, rows, fields) {
      rows.forEach(function(row) {
        console.log(row.name);
      });
    }, function () { console.log('Finish'); });
}
Community
  • 1
  • 1
Jack M.
  • 3,676
  • 5
  • 25
  • 36

1 Answers1

12

Put the console.log('Finish') statement after the forEach call:

npmSql.query("SELECT name FROM sqlTable", function(err, rows, fields) {
  rows.forEach(function(row) {
    console.log(row.name);
  });
  console.log('Finish');
});

You're using the standard javascript Array.prototype.forEach, which is synchronous. The question you linked deals with an unrelated async foreach function.

tcooc
  • 20,629
  • 3
  • 39
  • 57
  • the npmSql.query is wrapped in a function. Is it possible to return row.name value to that function ? check my updated question – Jack M. Aug 16 '14 at 12:51