Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS do stuff after fetching all mySql results

Tags:

node.js

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'); });
}
like image 480
Jack M. Avatar asked Aug 16 '14 09:08

Jack M.


1 Answers

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.

like image 148
tcooc Avatar answered Nov 15 '22 07:11

tcooc