Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to wait the end of a function including asynchronous MYSQL Queries from node.js?

I'm having Javascript problem to wait a function done before the below line is called. The previous function is including the Javascript MYSQL Queries calls (one of the library of node.js). Then it will be looks like:

function first() {
    /**
    * a lot processes to execute
    * including Asynchronous processes
    * like running Queries using Javascript MYSQL Library from node.js
    */
    console.log("I am the first one!");
}

first();
console.log("I am the second one!");

Then when i execute this, it happening like:

I am second one!
I am first one!

How do i make them run by keeping the queue order?

NOTE: Now for everyone who confusing the question, please jump/follow my newly created question again:
Everyone please follow/jump into this new question: Node.js MYSQL to detect the INSERT/UPDATE completeness of a Query?

like image 550
夏期劇場 Avatar asked May 21 '26 08:05

夏期劇場


1 Answers

Pass a callback to the 2nd function to the call to the 1st function. At the end of the 1st function, invoke theh callback:

function one(parm, callback) {
    // do something
    callback();
}
function two() {
    // do something else
}

one("my parm", two);
like image 130
Steve H. Avatar answered May 23 '26 21:05

Steve H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!