jquery-deferred

Waiting for multiple deferred objects to complete

只谈情不闲聊 提交于 2019-12-22 21:45:21
问题 When waiting for multiple deferred objects to complete, why does: $.when(tasks).then(function() { document.write("Completed all requests." + "<br/>"); }); execute immediately, yet $.when.apply(null, tasks).then(function () { document.write("Completed all requests." + "<br/>"); }); waits until the tasks have completed. 回答1: The when function does not take an array of deferreds. Rather, you pass each deferred as a separate argument. That's exactly what apply is doing for you. The null being

Waiting for multiple deferred objects to complete

只谈情不闲聊 提交于 2019-12-22 21:45:15
问题 When waiting for multiple deferred objects to complete, why does: $.when(tasks).then(function() { document.write("Completed all requests." + "<br/>"); }); execute immediately, yet $.when.apply(null, tasks).then(function () { document.write("Completed all requests." + "<br/>"); }); waits until the tasks have completed. 回答1: The when function does not take an array of deferreds. Rather, you pass each deferred as a separate argument. That's exactly what apply is doing for you. The null being

chain callback using deferred and pipe

余生长醉 提交于 2019-12-22 10:19:59
问题 I am struggling to chain the callback with pipes using deferred. It seems to work just fine but in the callback2, it brings me data from callback1. Here is how the code looks like: var getCall1 = function() { return $.ajax(url, { type: "GET", data: { }, contentType: "application/json", dataType: "json" }); } var getCall2 = function () { return $.ajax(url, { type: "GET", data: {}, contentType: "application/json", dataType: "json" }); } var callback1 = function (data) { alert('call 1 completed'

Error Handling and Recovery with jQuery Deferred

半腔热情 提交于 2019-12-22 08:26:09
问题 I am using jQuery and am aware that this issue is because the jQuery.Deferred implementation is not Promises/A+ compiant. I do not want to use any other libraries to solve this. With that out of the way, is there a way to recover from a $.Deferred().fail() callback such that I am returned back to the success chain? This is possible with the multi-callback form of then() but so far I have not found a solution using .fail() then: asyncThatWillFail().then(function () { // useless callback },

How to access return value from deferred object?

别来无恙 提交于 2019-12-22 05:54:22
问题 I have the following code that uses $.getJSON inside the repository to return some data that is then used by other functions. $.when( repository.getUserDetails().done(dataPrimer.getUserDetails), $.Deferred( function (deferred) { deferred.resolve(); } ) ).done( function () { repository.getUserPolicyTitles().done(dataPrimer.getUserPolicyTitles); }, function () { repository.getUserPage().done(); } ); This works but I need to return a value from: repository.getUserDetails().done(dataPrimer

Canceling a Deferred Promise in jQuery

▼魔方 西西 提交于 2019-12-22 03:54:07
问题 How can I cancel a promise without removing the element from the DOM? fiddle I ran this code: $("#box") .delay(2000) .show("slow") .delay(2000) .promise() .then(function(){log("Done");}); After this, is there a way to cancel the promise? Both clearQueue() and stop(true) didn't work, because it's not an animation that I'm trying to cancel. I saw that remove() should do it ... but I only want to stop the promise, not remove the entire element. 回答1: Good news. Since yesterday you can cancel your

jquery deferred - “always” called at the first reject

人盡茶涼 提交于 2019-12-22 03:48:29
问题 I'm using $.when to chain some Deferred objects, and if one of them fail, the always method will be called directly after the failure, even if I still have some deferrer in a "pending" state. var promises = [], defs = []; for(var i=0 ; i < 10 ; i++){ defs.push($.Deferred()); promises.push(defs[i].promise()); } var res = $.when.apply($, promises); res.fail(function(){console.log('failed')}); res.done(function(){console.log('done')}); res.always(function(){console.log('always')}); res.then

Ajax API calls in loop need to be executed in order

末鹿安然 提交于 2019-12-22 01:12:23
问题 lets say you have a scenario where you needed to create a .csv output in a page's textarea... So I have an array of queries which i loop. Inside the loop im passing a query to an ajax call... I need to append the result of the ajax call into the textarea. My question is how do you get the results printed out in the order they are requested (basically the order in the queries array) //example array to loop. var queries= ['query1', 'query', 'query3', 'query4']; //the textarea where im going to

Find first available data source with jQuery Deferred

空扰寡人 提交于 2019-12-21 17:21:54
问题 So I was asked this at an interview, but it brought up a good use case. Assume that you have a bunch of data sources. You want to find the first available one and process it and ignore the rest. So something like: var datasources = new Array("somedatabase1/pizza","somedatabase2/beer","somedatabase3/llama"); var dfds = new Array(); $.each(datasources,function(source){ dfds.push($.getJSON(source)); }); $.when(dfds).done(function(){alert("they are all done");}); Ignore that I really don't think

Provide a default 'fail' method for a jQuery deferred object

亡梦爱人 提交于 2019-12-21 12:46:14
问题 I am writing a Javascript API client using jQuery. My top level request method looks like this: function request(method, uri, params, proxies) { var deferred = $.Deferred(); $.ajax({ data: method == 'GET' ? params : JSON.stringify(params), contentType: 'application/json', dataType: 'json', url: api.root + uri, type: method, xhrFields: { withCredentials: true } }).done(function(body) { deferred.resolveWith(this, [body.data]); }).fail(function(xhr) { deferred.rejectWith(this, [xhr]); }); return