Live data from Hacker News

Multiple Simultaneous Ajax Requests (with one callback) in jQuery

css-tricks.com

11–20 of 31 posts

Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery

#11
post #4

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…

Thank you for clarifying this!

Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery

#15
post #10

Or 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…

Yes, you can reinvent wheels and avoid javascript best practices, and lose all benefits of async code control.

Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery

#16
post #10

Or 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…

A 'count-down latch' is commonly (at least when I learned it, using pthreads) called a 'Conditional Variable'.

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

#17
Can'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

#18
post #10

Or 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…

True, and this would work, but why would you favor this over Promises? They're an open, well-known spec with a lot of implementations - hell, some browsers support them natively. Promises also offer better control over sequential action and error handling.

Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery

#19
post #17

Can'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?

Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery

#20
post #17

Can'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?

Buy it and you'll find out... ;-)

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!"

Post reply on HN