Multiple Simultaneous Ajax Requests (with one callback) in jQuery
css-tricks.com
css-tricks.com
.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 allow multiple 'then's to be added, either before or after the promise resolves.
I'm working on a blog post about promises and related patterns (in the context / implementation of Angular, but they're equally valid for jQuery or other promise implementations), I'll post it on HN hopefully somewhere this weekend or early next week.
$.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.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 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!"
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.
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 latch-producing function so you can just write stuff like: var l = makeLatch(callback);
$.get("/a", l());
$.get("/b", l());
$.get("/c", l());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.
In your case case, your mutex would be the num_results variable and the conditional is that it is greater than 0.
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.
$.when.apply($, (object.request() for object in objects)).done (responses...) -> doStuff()
Also, if you are using promises for getter functions, you'll have to store those promises somewhere, so that next time you don't load them again. Basically "getting" a resource involves caching, promises, and maybe even throttling. So its ideal implementation is not really just a promise.
Check this out: http://platform.qbix.com/guide/patterns