最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 pleteness of a Query?

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 pleteness of a Query?

Share edited May 23, 2017 at 11:45 CommunityBot 11 silver badge asked Jun 8, 2012 at 14:51 夏期劇場夏期劇場 18.4k46 gold badges141 silver badges226 bronze badges 15
  • 7 No, it doesn't happen like that. – Andrew Whitaker 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. – Frédéric Hamidi 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 – Esailija 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". – cHao 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. – Esailija Commented Jun 8, 2012 at 15:18
 |  Show 10 more ments

3 Answers 3

Reset to default 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);

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"; });

The problem you are having is very mon 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!

发布评论

评论列表(0)

  1. 暂无评论