3

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?

15
  • 7
    No, it doesn't happen like that. Commented Jun 8, 2012 at 14:53
  • 2
    Does your first one line reside in an asynchronous callback? Because what you're saying cannot happen otherwise. Commented Jun 8, 2012 at 14:53
  • 3
    The code you have given is misleading, it's impossible for this to happen with that code Commented Jun 8, 2012 at 14:54
  • 2
    @4lvin: YES IT DOES, regardless. That's how JavaScript works. The only way "second" appears before "first" is if "first" is in a callback. As written, this code will always log "first", then "second". Commented Jun 8, 2012 at 15:18
  • 2
    @4lvin ok here we have about a dozen of async calls inside the first, they are irrelevant in this regard. If I were to put the log call inside a callback of one of those calls, then it would happen of course. But that's not what the code in the OP is doing. Commented Jun 8, 2012 at 15:18

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

You would need to structure your code to use a call back

function first (callback) {

// do your stuff

callback.call();
}

first(function () { console.log("I am the second"; });

Comments

1

The problem you are having is very common on people who had programmed in other languages before JavaScript, such as c/java, you think JavaScript will do the following:

 Line of code1. Execute,when it's done go to the next.
 Line of code2. Execute,when it's done go to the next.
 Line of code3. Execute,when it's done go to the next.

What actually happens in JavaScript is more like:

 Line of code1. Execute
 Line of code2. Execute
 Line of code3. Execute

For JavaScript to work as you expect you need to program it in a event oriented way, that means, you need to specify which functions you want run in what specific order. To do so in JavaScript you need to make use of callbacks, for example:

 function1 (parameter A,function2){
        //...   
        function2(parameter B,function3)} 

 function2 (parameter B,function3){//...} 

 function3 (){//...} 

You could generalize more the example above, however i think leaving it like this makes it easier for understanding. You can find many articles on the web about this. The first result of a google search gave me this link.

Happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.