Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use async false and async true in ajax function in jquery

Tags:

jquery

ajax

When to use use async false or async true in an ajax call. In terms of performance does it make any difference ?

example :

$.ajax({
        url : endpoint,
        type : "post",
        async : false,
        success : function(data) {
                if (i==1){  
                getMetricData(data)}

                else if (i==2)
                {
                    capture = data;
                }

        }
    });
like image 366
user2569524 Avatar asked Aug 21 '13 19:08

user2569524


People also ask

What does async false do in AJAX call?

jQuery Ajax Async False is a function of jQuery in which if we set async as false that means the first statement has to be complete before calling the next statement. If we set the async request as true, then the next statement will begin its execution whether the previous statement is completed or not.

What is use of async in jQuery AJAX?

The jQuery Ajax async is handling Asynchronous HTTP requests in the element. It is a procedure to send a request to the server without interruption. It is an Asynchronous method to send HTTP requests without waiting response. It is a function to working on a server without associating more than on request.

Is Ajax async false deprecated?

As of jQuery 1.8, the use of async:false in jQuery. ajax() is deprecated.

Can AJAX requests be made synchronous True False?

Synchronous AJAX call is made when async setting of jQuery AJAX function is set to false while Asynchronous AJAX call is made when async setting of jQuery AJAX function is set to true. Default value of the async setting of jQuery AJAX function is true.


3 Answers

It's not relative to performance...

You set async to false, when you need that ajax request to be completed before the browser passes to other codes:

<script>
    // ...
    $.ajax(... async: false ...); // Hey browser! first complete this request, 
                                  // then go for other codes

    $.ajax(...); // Executed after the completion of the previous async:false request.
</script>
like image 87
Amin Saqi Avatar answered Oct 19 '22 19:10

Amin Saqi


  1. When async setting is set to false, a Synchronous call is made instead of an Asynchronous call.
  2. When the async setting of the jQuery AJAX function is set to true then a jQuery Asynchronous call is made. AJAX itself means Asynchronous JavaScript and XML and hence if you make it Synchronous by setting async setting to false, it will no longer be an AJAX call.
  3. for more information please refer this link
like image 2
Apurv Chaudhary Avatar answered Oct 19 '22 19:10

Apurv Chaudhary


It is best practice to go asynchronous if you can do several things in parallel (no inter-dependencies). If you need it to complete to continue loading the next thing you could use synchronous, but note that this option is deprecated to avoid abuse of sync:

jQuery.ajax() method's async option deprecated, what now?

like image 2
Christophe Roussy Avatar answered Oct 19 '22 20:10

Christophe Roussy