Live data from Hacker News

Multiple Simultaneous Ajax Requests (with one callback) in jQuery

css-tricks.com

21–30 of 31 posts

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

#21
Please let me know if this is a horrible UI pattern (and I'd love to hear alternatives), but this could also be used to implement something like "do X, after Y succeeds and at least Z milliseconds have passed".

For example, if you have a Search button that makes an AJAX request, and there's a loading spinner while waiting for that request, you could use this technique to make the spinner show for at least, say 500 milliseconds. I have noticed that a lot of AJAX search forms return so quickly that the loading spinner just barely flickers before the results display. It might be less jarring if you ensured that the spinner would show for at least 500 ms. Of course, you don't just want to add a 500 ms delay after the request returns, because then slow requests will get that much slower.

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

#22
post #6
post #5

This is the proper solution: http://css-tricks.com/multiple-simultaneous-ajax-requests-on...

$.when( // Get the css-tricks once $.get("http://css-tricks.com"), // get it twice $.get("http://css-tricks.com") ).then(function( csstricks1, csstricks2 ) { console.log( 'Fetched css tricks' ); console.log( csstricks1, csstricks2 ); }); csstricks1[0] is the content object and usually what you'll want - for example, if the response is json.

Why would you fetch the same thing twice? Or is that just for you example and wouldn't be done in reality?

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

#23
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).

Loving the book, seriously the examples are easy to follow and realy well paced.

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

#24
post #6

Earlier quoted context omitted.

$.when( // Get the css-tricks once $.get("http://css-tricks.com"), // get it twice $.get("http://css-tricks.com") ).then(function( csstricks1, csstricks2 ) { console.log( 'Fetched css tricks' ); console.log( csstricks1, csstricks2 ); }); csstricks1[0] is the content object and usually what you'll want - for example, if the response is json.

Why would you fetch the same thing twice? Or is that just for you example and wouldn't be done in reality?

Just to be sure that one of them isn't corrupted.

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

#25
Like many, I had written myself a similar utility some years ago because "I need several stuffs that don't depend on each other, so they could be downloaded or calculated at the same time to get the end result faster" is a pattern that often comes up.

Nowadays I use "async.js" (https://github.com/caolan/async): does the same, but with extra features.

It doesn't rely on jQuery unlike the snipplets of the article, so it's useful when you're aiming for vanilla or want to share code with Node.

It's far from the only library of this type, but I like it because it's reliable and has a good balance of simple yet tweakable for uncommon workflows.

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

#26

Like many, I had written myself a similar utility some years ago because "I need several stuffs that don't depend on each other, so they could be downloaded or calculated at the same time to get the end result faster" is a pattern that often comes up. Nowadays I use "async.js" ( https://github.com/caolan/async ): does the same, but with extra features. It doesn't rely on jQuery unlike the snipplets of the article, so…

I also like promises-style solutions, but in practice it felt like it needed more things modified to work with them than async when mixing with third-party libraries required by projects, but that's more a gut feeling than backed by hard numbers, so I'll reevaluate that assertion when the right project comes along.

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

#27
post #23
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).

Loving the book, seriously the examples are easy to follow and realy well paced.

Thanks!

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

#28
post #23
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).

Loving the book, seriously the examples are easy to follow and realy well paced.

Great :-)

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

#29
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?

It's more a matter of learning how you can use them in various ways, and also of really getting into the habit of thinking with deferreds. I've been using them for about 7 years (mainly in Python) and I'm still finding interesting new ways to think about them, new tricks, etc. [Disclaimer: I'm also one of the book's authors.] BTW, if you use AUTHD as a discount code on the O'Reilly site, you'll get 50% off the e-book price or 40% off a dead tree version.

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

#30
post #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.

Latches aren't async code control? Javascript has (async) best practices that survive more than a couple years?

Promises are great, I totally agree (as long as they're A+ compliant). But they're not on every platform, and it's not (usually) worth building your own if it's not there.

Post reply on HN