Saturday, 4 March 2017

javascript - NodeJS wait for function to finish

How can i wait for a function to complete, before executing another function in NodeJS?



Let's say thats my main function with db querys etc.



function main() {
// Do some db querys here
connection.query(query, function(err, rows) {

if (err) throw err;
for(var i = 0; i < rows.length; i++) {
console.log('Got Row ' + i);
}
});
// Do some other stuff here
...
}



And that's the function which is get called after the main() function above is completely finished.



function waitForMain() {
console.log('Finally i was called!');
}


Is this possible with callbacks? Or are there other methods?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...