AJAX ASYNC False vs. True

I have a test site here (kdmalikdesign.com/test/rsd/index.html). I am in the middle of dealing with this. My main concern is that this does not work now, if ASYNC DOES NOT FALSE, what I hear is bad practice?

Now I have identified the reason why async is false, because when I run the success callback, the xml data does not load, but when I load the completed callback, everything loads fine. I had to change it to asynchronous false so that it would work correctly with the callback.

Is there a specific way to do this? Basically what ajax call does is grab the xml file and read through it depending on the name of the file filling the page with specific data. I do this mainly as a practice / exercise.

Thanks Kamron

+4
source share
3 answers

Making a synchronous call is usually a negligent practice, as you effectively execute the request, while losing all the benefits of asynchrony when you can use callbacks to do the same asynchronously.

async:false will call jQuery.ajax() until it returns. In fact, in the pseudo code instead:

 function ajax: perform request callback with results 

You do it:

 function ajax: perform request while (no results) wait return results 

This completely blocks the execution of anything else until it ends ... which is pretty terrible. An obvious use case is to launch an element in the waterfall template: task 1 -> task 2 -> task 3 , which can happen.

If you can afford to block your browser, consider using callbacks anyway. They will allow you to keep the rest of your site active and well, when processing material. You can easily do this by setting async:true and providing the callback with the next step. However, you may find yourself in a callback spaghetti and you might want to use the library to manage large operations, if you have any.

A very good candidate for this is welcomed by Node.JS and is called async.js . This is a tool for MapReduce files and implements both waterfall and parallel models.

The moral of the story : async: false can replace 100% of the time with a callback.

+5
source

If you want to implement asynchronous calls, then you want to organize all the logic that needs to be executed with the resulting data in the .succcess () callback.

the reason async is false is because when I run the success callback, the xml data is not loading, but when I load the completed callback, everything loads fine.

This line from your question is a bit confusing, because by definition success callback is executed after the request is completed and any data is returned.

This is an example of what you cannot do with async: true :

 function ajaxrequest() { var someValue; $.get('serverFile.php', function(data){ someValue = data; }); return someValue; } 

In this case, the someValue variable will be empty when it is returned, because the callback you called is not executed synchronously or on a line with the rest of your code.

To use async:true , you can arrange your code as follows:

 function getData() { return $.get('serverFile.php'); } function alertData() { getData().done(function(data){ alert(data); }); } function logData() { getData().done(function(data){ console.log(data); }); } 

I broke it up so that you can see that it can be useful to isolate your query methods and return them jQuery Deferred so that several other functions can use the same query if necessary.

+1
source

async true with the same location of the request makes it critical for data overflow, especially in the database request, it will cause too many connection problems, especially if the request is repeated

and the second - the nth request will respond longer

0
source

Source: https://habr.com/ru/post/1480982/


All Articles