jquery-deferred

Is any jQuery version compliant to Promise/A specifications?

谁说胖子不能爱 提交于 2019-11-28 11:00:01
After going through several articles I have come to know that promise implementation is there in jQuery. But I am not sure whether any version of jQuery is Promise/A compliant or not. Benjamin Gruenbaum Update 2015: jQuery 3.0 is Promises/A+ compatible. See this issue on GitHub so 3.0 beta is 3.0 compatible and when 3.0 is out it will also be compatible. Until then - the below still applies. All jQuery versions (up to 3.0) feature a broken promise implementation They don't allow error handling well, and they don't mix and match with other implementations well. However, since version 1.8 .then

jQuery deferred - do I need pipes or chains to achieve this pattern?

爱⌒轻易说出口 提交于 2019-11-28 08:47:21
I'm trying to implement the folowing scenario, using JQuery deferred, without much luck. What parts of the deferred api would you use, and how would you structure your calls to achieve the following: 1st ajax callA to serviceA retrieve a list of Ids wait until this call returns then n ajax calls to serviceB, each call using a using an Id from the list returned by callA wait until all serviceB calls have returned then a final ajax call to serviceC You could do like this (more or less pseudocode): (function() { // new scope var data = []; // the ids coming back from serviceA var deferredA =

Using jQuery load with promises

与世无争的帅哥 提交于 2019-11-28 08:20:35
I'm still trying to wrap my head around deferred and what not, so with this in mind I have a question on how to do the following. My team and I have 3 separate .load() methods that each go grab a specific template and append that to the same container. Each load takes a different amount of time as you might think, so when the content loads, it loads in a 'stair step' fashion (1, then 2, then 3). I'd like to make use of deferred objects and wait until they are all done, then append them at the same time to remove the 'stair step' action. $('<div>').load(baseInfoTemplate, function () { var

Asynchronous Loop of jQuery Deferreds (promises)

荒凉一梦 提交于 2019-11-28 05:25:38
I am trying to create what I think is referred to as a "Waterfall". I want to sequentially process an array of async functions (jQuery promises). Here's a contrived example: function doTask(taskNum){ var dfd = $.Deferred(), time = Math.floor(Math.random()*3000); setTimeout(function(){ console.log(taskNum); dfd.resolve(); },time) return dfd.promise(); } var tasks = [1,2,3]; for (var i = 0; i < tasks.length; i++){ doTask(tasks[i]); } console.log("all done"); I would like it to complete the task in the order they are executed (present in the array). So, in this example I want it to do task 1 and

javascript function wait until another function to finish

夙愿已清 提交于 2019-11-28 04:25:36
I have two javascript functions that are called from android. After long debug sessions finally I realized that the problem is arising from the fact that second function is getting called before first one is finished. I already searched the examples with deferred etc, but they all depends on function calls within another one. function FunctInit(someVarible){ //someVariable is sent from android, cannot call again from getResult //init and fill screen } function getResult(){ //also getResult need to be called from android via button //return some variables } How can I force getResult to wait

Retry a jquery ajax request which has callbacks attached to its deferred

风格不统一 提交于 2019-11-28 03:40:43
I'm trying to implement a system of retrying ajax requests that fail for a temporary reason. In my case, it is about retrying requests that failed with a 401 status code because the session has expired, after calling a refresh webservice that revives the session. The problem is that the "done" callbacks are not called on a successful retry, unlike the "success" ajax option callback that is called. I've made up a simple example below: $.ajaxSetup({statusCode: { 404: function() { this.url = '/existent_url'; $.ajax(this); } }}); $.ajax({ url: '/inexistent_url', success: function() { alert(

jQuery Deferred not calling the resolve/done callbacks in order

喜夏-厌秋 提交于 2019-11-28 01:55:06
Code example: http://jsfiddle.net/MhEPw/1/ I have two jQuery Deferred objects. I want to have more than one 'async' request happening - and after they all run I want the callbacks (the .done functions) to be run in order they were specified in. Unfortunately they don't run in order. Maybe I am looking for some functionality here that Deferred doesn't provide? What you need to do is link all of your request with one master deferred object and register all of your callbacks on its promise. The master deferred object would need to listen to the individual requests and resolve accordingly. The

Check Internet connectivity with jquery

青春壹個敷衍的年華 提交于 2019-11-27 22:26:09
I am trying to check for the internet connection by sending a GET request to the server. I am a beginner in jquery and javascript. I am not using navigator.onLine for my code as it works differently in different browsers. This is my code so far: var check_connectivity={ is_internet_connected : function(){ var dfd = new $.Deferred(); $.get("/app/check_connectivity/") .done(function(resp){ return dfd.resolve(); }) .fail(function(resp){ return dfd.reject(resp); }) return dfd.promise(); }, } I call this code in different file as: if(!this.internet_connected()) { console.log("internet not connected

jQuery $.when() with variable arguments

送分小仙女□ 提交于 2019-11-27 22:01:12
I want to send [1, n) AJAX-requests to the server and after all have returned a result a modal dialog should be closed. $.when(a(), b(), c()) would be perfect, but I don't know how to pass the variable count of functions to $.when as parameter. Any ideas how to solve this problem? Felix Kling Call the functions and add their return values to an array. Then call $.when passing the array as argument like so: $.when.apply($, array) See Function.prototype.apply [MDN] for more information and my answer here for an extended example. 来源: https://stackoverflow.com/questions/8011652/jquery-when-with

Can I force jQuery Deferred/Ajax to execute fail handler after it has been resolved?

南楼画角 提交于 2019-11-27 18:16:32
问题 An example to make clear what i want to do. This is what I would usually do: function success(data, status, jqxhr){ if ( data.error ) return failure(jqxhr, status, data.error); // process data } function failure(jqxhr, status, err){ ... } $.ajax( ... ) .done(success) .fail(failure) Is there any way, i can accomplish this with anonymous functions only, like so? $.ajax( ... ) .done(function(data, status, jqxhr){ if(data.error) // what do i need to do here to jump in to the fail handler? })