Is $.get of jquery asynchronous?

一世执手 提交于 2019-11-27 21:59:26

Yes, $.get is asynchronous. From the documentation:

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

...and as that doesn't have the async option, async defaults to true. (Note that async will be going away entirely in a future version of jQuery; it will always be true.)

If $.get is asynchronous then what's the point of $.ajax?

$.ajax gives you control over lot more options. $.get is just a shortcut.

$.get is simply a shorthand for:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

As stated by the jQuery documentation, $.get is a convenience wrapper for the more low-level $.ajax.

$.ajax with GET method, How about this with a promise object?

function showResults(name) { 
        var deferred = $.Deferred, requests = [];

        requests.push($.ajax({url:"/path/to/uri/?name=" + name, type: "GET", async: false}).done(function(d) { 
         //alert or process the results as you wish 
        }));
        $.when.apply(undefined, requests).then(function(d) { console.log(d); /*var d is a promised late return */ deferred.resolve(); }); 
        return deferred.promise();

    }

he returned promise object can also be used with $.when(showResults('benjamin')).done(function() { }); for post modifications (like chart/graph settings etc). totally reusable. You may also put this function in a loop of $.deferred requests like,

function updateResults() { 
             var deferred = $.Deferred, requests = [];
             requests.push($.ajax(url:"/path/to/names/?nameArr=" + jsonArrOfNames, type: "GET", async: false}).done(function(res) {  requests.push(showResults(res[0]));}) );
             $.when.apply($, requests).then(function(d) { console.log(d); /*var d is a promised late return */  deferred.resolve(); }); 
             return deferred.promise();
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!