1

I'm learning to perform a callback after successful results from two functions, one of which is ajax and one is non-ajax (both are asynchronous). In my script there is a non-ajax call to load multiple images at the start of the program, and an ajax (JQuery) call to request a JSON with data for a variable in the script. I've discovered JQuery's when() function for making multiple ajax calls, and I've discovered this answer for making multiple requests before a callback, that might be hackable. I'm chasing my tail a bit on this one and looking for some advice on whether or how to make a function to perform a callback after multiple, mixed functions. Thanks in advance for your thoughts!

Community
  • 1
  • 1
gromiczek
  • 2,970
  • 5
  • 28
  • 49

1 Answers1

2

$.when will actually take in multiple deferred objects, so you can do something like this:

var xhr = ajax();
var images_promise = loadImages();

$.when.apply($, [xhr, images_promise]).done(function () {
    // something to do when both are complete
});

Provided that the ajax and loadImages functions return promise objects:

function ajax() { 
    return $.ajax({
        // ajax configuration
    });
}

function loadImages() { 
    // create the deferred promise object
    var dfd = new jQuery.Deferred();
    $("img").on('load', function() { 
        // on the load event resolve the promise
        dfd.resolve();
    });
    return dfd;
}

Read more about deferred promises here

Dan-Nolan
  • 6,594
  • 4
  • 27
  • 32
  • Ok, I think I'll need to do some reading on promise objects - I don't yet think there's a promise object in the loadImages function I wrote. – gromiczek Apr 15 '14 at 14:06
  • @gromiczek Sounds good. I've added a very simple example of using deferred promises below for reference. Good luck! – Dan-Nolan Apr 15 '14 at 14:10
  • So how do I return something (images, or the JSON) from each of these functions after the deferred object has been resolved? Is there a property in the object that holds the returned data? I'm sure this is somewhere in the documentation... – gromiczek Apr 15 '14 at 15:17
  • It should be in full detail in the deferred promises link at the bottom of the post. Basically you'll want to pass data through the deferred object's resolve method. Something like `dfd.resolve({name: 'Dan'});`. Then in the `.done(p1,p2)` method, the parameters `p1`, `p2` will be the data you passed through the resolve method and the data returned with the ajax call. – Dan-Nolan Apr 15 '14 at 15:51
  • @gromiczek If possible, post `html`, `css` `js` ? Perhaps, see also http://stackoverflow.com/questions/22572795/how-to-find-if-the-chained-asynchronous-scripts-has-been-loaded/ ? – guest271314 Apr 15 '14 at 18:02
  • The script alone without dependencies is over 700 lines, so too big for here. I suspect your help above and comments here will get me going. I'm getting things strung together - weaving all this with my other related question at - http://stackoverflow.com/q/23066488/2441384 – gromiczek Apr 15 '14 at 18:06
  • A fiddle that works more as a snapshot to see what I'm doing, rather than a functioning block of code - http://jsfiddle.net/gromiczek/xr45v/ – gromiczek Apr 15 '14 at 20:26
  • Pheeew. Finally got it!! Thanks so much. – gromiczek Apr 16 '14 at 15:10