globalStore? Ewwwww. As the jQuery documentation states on $.when [1], the .then() callback function will be called with a number of arguments equal to those passed to $.when(), ergo in this example: .then(function(html, css, feature) {}); Which avoids the need for a global / shared object. If you need access to these resources outside of this one callback, prefer to pass the promise object itself around; promises al…
Multiple Simultaneous Ajax Requests (with one callback) in jQuery
11–20 of 31 posts
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#12Promises are the way you should be doing AJAX, really.
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#13Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#14Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#15Or you can make a count-down latch of some kind: var num_results = 3; var results = [] var makeCallback = function(index) { return function(response) { num_results -= 1; results[index] = response; if (num_results > 0) { return; } // actually do stuff here, all results are loaded. }; }; $.get("/a", makeCallback(0)); $.get("/b", makeCallback(1)); $.get("/c", makeCallback(2)); // etc It's also not hard to make a generic…
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#16Or you can make a count-down latch of some kind: var num_results = 3; var results = [] var makeCallback = function(index) { return function(response) { num_results -= 1; results[index] = response; if (num_results > 0) { return; } // actually do stuff here, all results are loaded. }; }; $.get("/a", makeCallback(0)); $.get("/b", makeCallback(1)); $.get("/c", makeCallback(2)); // etc It's also not hard to make a generic…
In your case case, your mutex would be the num_results variable and the conditional is that it is greater than 0.
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#17http://shop.oreilly.com/product/0636920030508.do
...has just been published.
(N.B. I'm one of the authors - all feedback would be most welcome and apologies for the shameless plug).
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#18Or you can make a count-down latch of some kind: var num_results = 3; var results = [] var makeCallback = function(index) { return function(response) { num_results -= 1; results[index] = response; if (num_results > 0) { return; } // actually do stuff here, all results are loaded. }; }; $.get("/a", makeCallback(0)); $.get("/b", makeCallback(1)); $.get("/c", makeCallback(2)); // etc It's also not hard to make a generic…
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#19Can't get enough of deferreds and promises? Want lots more examples of interesting, cunning and mind bending ways to use promises and deferreds? As luck would have it, this: http://shop.oreilly.com/product/0636920030508.do ...has just been published. (N.B. I'm one of the authors - all feedback would be most welcome and apologies for the shameless plug).
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#20Can't get enough of deferreds and promises? Want lots more examples of interesting, cunning and mind bending ways to use promises and deferreds? As luck would have it, this: http://shop.oreilly.com/product/0636920030508.do ...has just been published. (N.B. I'm one of the authors - all feedback would be most welcome and apologies for the shameless plug).
I don't mean to shit on your book, but an entire book dedicated to jQuery deferreds? There is that much to say about them?
I wrote this high-level piece of fluff as a taster for the Guardian's developer blog:
http://www.theguardian.com/info/developer-blog/2013/sep/17/d...
"If Doctor Who were a programmer, he'd use deferreds!"