When should I use jQuery deferred’s “then” method and when should I use the “pipe” method?

Since jQuery 1.8 .then behaves the same as .pipe: Deprecation Notice: As of jQuery 1.8, the deferred.pipe() method is deprecated. The deferred.then() method, which replaces it, should be used instead. and As of jQuery 1.8, the deferred.then() method returns a new promise that can filter the status and values of a deferred through a function, … Read more

jQuery.when – Callback for when ALL Deferreds are no longer ‘unresolved’ (either resolved or rejected)?

I think the easiest way to do this is to keep a secondary Deferred object around for each AJAX request, and ensure that that one is always resolved: var d1 = $.Deferred(); var d2 = $.Deferred(); var j1 = $.getJSON(…).complete(d1.resolve); var j2 = $.getJSON(…).complete(d2.resolve); $.when(j1, j2).done(function() { // only fires if j1 AND j2 are … Read more

$.Deferred: How to detect when every promise has been executed

More sophisticated promise libraries have an allSettled() function like Q or Promise.settle like Bluebird. In jQuery, you could implement such a function yourself as well and extend the $ namespace with it, but that will only be necessary if you need it often and performance-optimized. A simpler solution would be to create a new promise … Read more

Reactjs async rendering of components

There are two ways to handle this, and which you choose depends on which component should own the data and the loading state. Move the Ajax request into the parent and conditionally render the component: var Parent = React.createClass({ getInitialState: function() { return { data: null }; }, componentDidMount: function() { $.get(‘http://foobar.io/api/v1/listings/categories/’).done(function(data) { this.setState({data: data}); … Read more

How do you work with an array of jQuery Deferreds? [duplicate]

You’re looking for $.when.apply($, promises).then(function(schemas) { console.log(“DONE”, this, schemas); }, function(e) { console.log(“My ajax failed”); }); This will also work (for some value of work, it won’t fix broken ajax): $.when.apply($, promises).done(function() { … }).fail(function() { … });` You’ll want to pass $ instead of null so that this inside $.when refers to jQuery. It … Read more

Problems inherent to jQuery $.Deferred (jQuery 1.x/2.x)

Update: jQuery 3.0 has fixed the problems outlined below. It is truly Promises/A+ compliant. Yes, jQuery promises have serious and inherent problems. That said, since the article was written jQuery made significant efforts to be more Promises/Aplus complaint and they now have a .then method that chains. So even in jQuery returnsPromise().then(a).then(b) for promise returning … Read more