How do I set a specific order of execution when asynchronous calls are involved?

后端 未结 4 801
[愿得一人]
[愿得一人] 2021-01-07 08:13

I am new(2 days!!) to the world of JavaScript and my only prior coding experience is in Java where execution of statements takes place sequentially. I understand that or at

4条回答
  •  佛祖请我去吃肉
    2021-01-07 08:54

    A lot of what you've said is wrong. JavaScript is sequential in the same way as Java, but asynchronous calls are made more often. If you want your callback to be called after the long thing, you must call it after the long running program. Like so -

    console.log("Beginning");
    function Test(callback){
       setTimeout(function(callback){
        console.log("Something that takes a lot of time");
        callback();
       },5000);
    
     }
    function tstCallBack(){
       console.log("Should come last");
    }
    Test(tstCallBack);
    

提交回复
热议问题