JavaScript flow or execution


If I have a JS function as follows;

function testFn()
x.ajaxMethod(param1,JScallBackFunction); //Please do not worry about the syntax..this just indicates an external method call
alert("Line after ajaxMethod");

The ajaxMethod(), lets say is some kind of method defined in an external Java file (so it can be through DWR or anything) which returns some data…Point is it takes some time to execute this line of code…

Now my question is when will the alert on next line get fired (i.e. alert("Line after ajaxMethod");)

  1. Will it wait for these 2 things to complete (ajaxMethod execution as well as JScallBackFunction)
  2. It will be fired immediately without waiting for any of the above 2 things to complete ?

Also if you could guide in general about the JavaScript method flow execution, that will be great.


It depends. Ajax calls are usually asynchronous which means the execution of code will not be paused until the asynchronous function returns. Therefore the alert will be executed immediately.

Asynchronous functions in javascript are usually to do with Ajax and loading something from a remote server. If you do wish to force JavaScript to wait while loading that content then you can set a flag for the XMLHTTPRequest object.

this is a good question to read: When is JavaScript synchronous?

Answered By – Roman

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published