jquery-deferred

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

淺唱寂寞╮ 提交于 2019-11-29 04:22:11
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? }) .fail(function(jqxhr, status, err){ ... }) what do i need to do here to jump in to the fail handler? Don't

raising jquery deferred.then() once all deferred objects have been resolved

梦想的初衷 提交于 2019-11-29 01:36:05
问题 i have two javascript functions, save() and saveAll() , set up as below: function save(data) { return $.post('/save', data); } function saveAll(callback) { var dataArray = []; $.each(dataArray, function() { save(this); }); callback(); } i'm interested in modifying saveAll() so that it leverages jquery deferred objects, and raises the callback function once all save() operations have completed. however, i'm unsure of the exact syntax... specifically with relation to the $.each() inside of the

How do I chain a sequence of deferred functions in jQuery 1.8.x?

与世无争的帅哥 提交于 2019-11-29 01:20:12
Given these functions: function func1() { var dfd = $.Deferred(); setTimeout(function() { dfd.resolve('Password'); }, 1000); return dfd.promise(); } function func2(message) { var dfd = $.Deferred(); setTimeout(function() { if (message == 'Password') { dfd.resolve('Hello World'); } }, 1000); return dfd.promise(); } I'd like to find a better way to do the following. Note this is using jQuery 1.8.x. var promise = func1(); promise.done(function(message1) { var promise2 = func2(message1); promise2.done(function(message2) { alert(message2); }); }); Any ideas? I thought using jQuery #pipe or #then

Waiting for jQuery AJAX response(s) [duplicate]

旧巷老猫 提交于 2019-11-29 00:56:49
问题 This question already has an answer here: Wait until all jQuery Ajax requests are done? 20 answers I have a page that, using jQuery .ajax that is called 100 times (async: true), the problem is that, when they they are all being loaded, I need the system to wait for ALL 100 calls to return before continuing. How would I go about this? Thanks in advance! :) Update: These calls are made in a for() loop (there's 100 of them :)) 回答1: The nice way to do this is with $.when. You can use this as

jQuery deferred object with nested ajax calls

爷,独闯天下 提交于 2019-11-29 00:24:58
问题 I have a situation in which my ajax calls must perform in a particular order. I have used jQuery Deferred objects in other situations, but cannot seem to find a way to make this behave appropriately. I have a function which performs a number of ajax requests in it's lifetime. Some of the requests will be performed during the success callback of other requests. My question: is there a way to return all nested deferred objects to the original $.when call? A simplified example would be: function

How do I use jQuery promise/deffered in a custom function?

筅森魡賤 提交于 2019-11-28 22:11:23
问题 I have a function that gets the location through navigator.geolocation : var getLocation = function( callback ){ navigator.geolocation.getCurrentPosition( callback || function( position ){ // Stuff with geolocation }); }; I would like to make it so that I could chain this function using jQuerys' Deffered object but I have still not managed to grasp the concept and usage of Deffered. I'm looking for something similar to this Pseudo Code : getLocation().then(function(){ drawMarkerOnMap(); });

How to prevent the white 'flash' on page load created by background image loading delay?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 17:04:33
问题 The problem is, on most sites on the web, there are background images. They take time to load. Ordinarily, it wouldn't be a problem if the images were optimized, and small enough. However, on some of my sites, the javascript files find their way to load before anything else on the page, even though they're in the footer ! This creates a white "flash" before the background image loads. Why is my javascript loading first before anything else? I'm having this problem on many sites, and I see it

Chain multiple “then” in jQuery.when

老子叫甜甜 提交于 2019-11-28 16:22:44
I have a function that does something like this: function do_something() { // some code return $.when(foo, bar, baz).then(do_something_else); } function do_something_else(_foo, _bar, _baz) { // do something else return /* the original inputs */; } So, when someone uses do_something , they can also chain more callbacks, like: do_something().then(function(_foo_2, _bar_2, _baz_2) { console.log(_foo_2, _bar_2, _baz_2); }); The problem is that I don't know how to bypass the original return from do_something_else to the anonymous function described. I don't want to receive a list, but positional

How to return accumulated returned Promise values as array to .then() following Array.prototype.reduce()?

老子叫甜甜 提交于 2019-11-28 14:19:35
问题 Given this pattern someArray.reduce(function(p, item) { return p.then(function() { return someFunction(item); }); }, $.Deferred().resolve()).then(function() { // all done here // access accumulated fulfilled , rejected `Promise` values }, function err() { }); what approaches are possible to return accumulated values of fulfilled , rejected Promise objects to .then(fulfilled) as an array following call to .reduce() ? function someFunction(index) { console.log("someFunction called, index = " +

How can I determine if a jQuery object is deferred?

血红的双手。 提交于 2019-11-28 11:52:17
If I have a function that sometimes returns a deferred object but sometimes a non-deferred object. How can I tell which one it is? Depending on your use case, you could also use jQuery.when [1]: If a single argument is passed to jQuery.when and it is not a Deferred, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. With jQuery.when you can treat your mysterious object always as deferred: // x could be a deferred object or an immediate result var x = getMysteriousObject(); // success will be called when x is a deferred object and has been