Over a million developers have joined DZone.
Platinum Partner

How to Run Multiple AJAX Requests

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix. Download this Forrester Report to gain a better understanding of the low-code platform market and how to make a strategic platform selection you won’t regret.

 

Suppose you have a list of resources that you would like to retrieve by means of AJAX. `$.ajax()` (or it's short variants like `$.get()` or `$.post`) is usual way of making AJAX calls, it works great than you need to fetch one.

var url = '/api/resource/1';
$.get(url, function (r) {
    // use response
});

Even in case of several it might be still usable,

var url1 = '/api/resource/1';
var url2 = '/api/resource/2'
$.get(url1, function (r) {
    // use response from url1
 
    $.get(url1, function (r) {
        // use response from url2
    });
});

You can easily see, if you have more than 3 you are trapping into "callback hell". Furthermore, if you have a list of url's to fetch and the size of that list is dynamic, it's not even possible to build structure like that.

Not long time a ago, I've been exactly into this situation. So, I have a list of resources to fetch, I need to issue them one-by-one and I want to have only one callback, that would pass all fetched resources in one object. Initially I thought it's not even possible, at least with-out creation of some ugly code. But with great help of my colleagues the problem been solved.

jQuery Deferred Object is something I've head about, but never got a change to play with. It turn's out to be very nice and simple idea. Deferred allows you to build chainable constructions. Actually, `$.ajax()` always returns deferred object, so you can apply `.done()`, `.fail()` functions on it.

Here is the code, that you could be re-usable in the same situation;

var pipedAjaxRequests = function (urls, callback) {
 var responses = {};
 
 var promise = $.Deferred().resolve();
 _.each(urls, function (url) {
  promise = promise.pipe(function () {
   return $.get(url);
  }).done(function (response) {
   responses[url] = response;
  });
 });
 
 promise.done(function () {
  callback(responses);
 }).fail(function (err) {
  callback(responses, err);
 });
};

It does create the pipe of `$.get()` calls and place the responses in one response object. At the time then all resources are fetched, the callback is called. In case of errors, second parameter of callback will have error info.

Thanks a lot to @antsamar and @alex_gonchar for helping me out.


The Web Dev Zone is brought to you in partnership with Mendix. Better understand the aPaaS landscape and how the right platform can accelerate your software delivery cadence and capacity with the Gartner 2015 Magic Quadrant for Enterprise Application Platform as a Service.

Topics:

Published at DZone with permission of Alexander Beletsky , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}