Specifically, how does it differ from the default ( async: true
) ?
In what circumstances would I want to explicit set async
to fals
Setting async to false means the instructions following the ajax request will have to wait for the request to complete. Below is one case where one have to set async to false, for the code to work properly.
var phpData = (function get_php_data() {
var php_data;
$.ajax({
url: "http://somesite/v1/api/get_php_data",
async: false,
//very important: else php_data will be returned even before we get Json from the url
dataType: 'json',
success: function (json) {
php_data = json;
}
});
return php_data;
})();
Above example clearly explains the usage of async:false
By setting it to false, we have made sure that once the data is retreived from the url ,only after that return php_data; is called