0

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?

Luca Huber
  • 117
  • 1
  • 1
  • 9

0 Answers0