How to sync JavaScript callbacks?

前端 未结 6 1394
长发绾君心
长发绾君心 2020-12-01 08:54

I\'ve been developing in JavaScript for quite some time but net yet a cowboy developer, as one of the many things that always haunts me is synching JavaScript\'s callbacks.<

6条回答
  •  Happy的楠姐
    2020-12-01 09:12

    single thread is not always guaranteed. do not take it wrong.

    Case 1: For example, if we have 2 functions as follows.

    var count=0;
    function1(){
      alert("this thread will be suspended, count:"+count);
    }
    function2(){
      //anything
      count++;
      dump(count+"\n");
    }
    

    then before function1 returns, function2 will also be called, if 1 thread is guaranteed, then function2 will not be called before function1 returns. You can try this. and you will find out count is going up while you are being alerted.

    Case 2: with Firefox, chrome code, before 1 function returns (no alert inside), another function can also be called.

    So a mutex lock is indeed needed.

提交回复
热议问题