开发者

Is that asynchronous of any javascript calling?

开发者 https://www.devze.com 2023-03-25 01:48 出处:网络
I am quiet new to javascript, can not understand that why all javascript calls are asynchronous, for example, we have calling with order like

I am quiet new to javascript, can not understand that why all javascript calls are asynchronous,

for example, we have calling with order like

call_function1;

call_function2:

if function2 is depend on the results of funct开发者_运维百科ion1, that can not be ensured, because the execution is asynchronous. Is that true ? And why ?

If true, how to ensure they are synchronous.

If this is duplicated question, I am sorry, because it is quiet new for me.

Thanks fo your answer first.


JavaScript calls are synchonous. If second function depends on results of first function, you might use callbacks model.

function foo(callback) {
  var results = // get some results;
  callback(results);
}

function boo(results) {
  // do something with results here..
}

foo(boo);


No Javascript has functions that guarantee order and behave much like other languages. For example:

function f1() {
  alert(1);
}

function f2() {
  alert(2);
}

f1();
f2();

You will always get 1 and then 2. What's more is AFAIK javascript runs on one thread so you don't have to worry about race conditions either.

The asynchronous part of javascript comes from waiting on events. For example if you make 2 ajax requests (the a in ajax standing for asynchronous is a hint), you cannot guarantee which will come back first and thus if you have different callbacks for the two requests, you can't guarantee which will be called first.


Calling two functions in a row is definitely not asynchronous.

In fact.. javascript is 100% synchronous. Unless you use 'web workers' all javascript will always run in a single thread, in a single process. There is never, ever a situation where 2 scripts are running at the same time.

Even if you handle an event, coming from an XMLHTTPRequest for instance, the event will only be triggered after all other javascript code has stopped executing.


As mentioned in the comments to your question, JavaScript function calls are usually not asynchronous. The following will be executed in order:

function sayHello() {
   console.log("Hi");
}
function sayBye() {
   console.log("Bye");
}
sayHello();
sayBye();

The above code will first print "Hi", then "Bye", as the calls to sayHello and sayBye are in that order.

However, if a function does perform some action asynchronously, and you have another function that relies on the result of that, you can supply the second function as a callback to the asynchronous request. For example:

xmlHttpRequestObj.onreadystatechange = function() {
   if(xmlHttpRequestObj.readyState == 4 && xmlHttpRequestObj.status == 200) {
      //Asynchronous call returned successfully. Do something that relies on that here.
   }
}


I don't know how you say that "Javascript is generally said to be asynchronous". It is generally the opposite: Javascript is almost always not asynchronous.

So generally (no ajax, no callback, no event handling), the following functions calls will execute sequentially.

function1();
function2();

If you are assigning both these functions to a single event, both will be called concurrently, but will not be executed in parallel. Same if both are called at the same time (using setTimeout), or as an Ajax callback. See Is JavaScript guaranteed to be single-threaded? for a very good explanation.

You could have heard that Ajax is asynchronous. That is true when you consider the request to servers without interfering with the user, "in the background". But this does not mean that Javascript is asynch. In fact even with Ajax calls, the javascript part is single threaded.

0

精彩评论

暂无评论...
验证码 换一张
取 消