jquery-deferred

What's the equivalent of jQuery.when() in angular

↘锁芯ラ 提交于 2019-12-07 04:14:01
问题 In jQuery we can do $.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) { ... }); What's the equivalent in angular? I really need to wait for all ajax calls finish then do stuff. Thanks. 回答1: You can use $q.all to handle multiple promises. Also, use $http to make the calls, that's more angular. Here is a nice tutorial: https://egghead.io/lessons/angularjs-q-all Hope that helps. 回答2: The equivalent would be: $q.all([$http.get('/page1.php'),$http.get('/page2.php')])

How do you attach callbacks to the deferred object that is attached to a deferred object?

对着背影说爱祢 提交于 2019-12-06 07:04:48
I have a chain of ajax requests that support a series of cascading drop down select lists. When you select a value in the 1st drop down, a request is fired to populate the 2nd, when that is complete (and the drop down filled), the next request fires to populate the 3rd drop down, and so on down the line. There are some variations to the way these request chains are formed, so I was hoping to assemble the requests using the jQuery Deferred objects. I see how I can chain the 2nd request to the first, but I don't see how I can chain the third request to the 2nd. function Step1() { return $.ajax(

jquery-ajax multiple calls

混江龙づ霸主 提交于 2019-12-06 06:06:10
I'm using the following code to have multiple ajax requests like this: request 1 start | request 1 finish | request 2 start | request 2 finish | ... This is the code: var startingpoint = fireRequest(1); $.each(types,function(ix,type) { startingpoint = startingpoint.pipe( function() { alert(startingpoint.status); // undefined return fireRequest(type); }); }); fireRequest is just a switchcase to the correct ajax function which returns $.ajax(...) I'd like the chain to stop when one request fails. I started implementing it and as a test I'd like to alert the status of the ajax object, but it

Declare jQuery AJAX call for execution later

大城市里の小女人 提交于 2019-12-06 04:57:37
I could declare a jQuery AJAX call this way: var foo = $.ajax({ ... }); But that would actually perform the request right then and there, correct? How can I declare the AJAX call without executing it initially, then call it later? Something like: var foo = $.ajax({ ... }); // some stuff in between foo.execute(); Thanks. EDIT Little more info: What I really want to do is have a function that constructs an AJAX request based on parameters, returns it to the calling code, and have the calling code manage its state (i.e. be able to execute it, abort it, etc.). So rather than simply declare the

How to fetch value from Promise object after promise has been resolved

孤人 提交于 2019-12-05 19:01:34
问题 Please note This is a contrived example. function longFunc(){ var deferred = $.Deferred(); setTimeout(function(){ console.log("long func completed"); deferred.resolve("hello"); }, 3000); return deferred.promise(); } function shortAfterLongFunc(x){ console.log('short func completed with value: ' + x); return { a: x }; } processFurther(longFunc().then(shortAfterLongFunc)); // send the array for further processing Problem I am unable to figure out how to return any kind of object/function for

Error Handling and Recovery with jQuery Deferred

半城伤御伤魂 提交于 2019-12-05 13:33:42
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 }, function () { console.log("error"); return $.Deferred().resolve(); }).then(function () { console.log(

jQuery deferred behaviour in for loop

瘦欲@ 提交于 2019-12-05 02:47:16
问题 I recently asked a question about the behaviour of jquery deferred in a for loop. Link here I received a working answer but I don't understand why it works. If I have the following code: function update(callbacks) { return $.Deferred(function(dfr) { setTimeout(function() { callbacks.success() }, 1000); dfr.resolve(); }).promise(); } function updateElements(deferreds) { for (var i = 0; i < 5; i++) { (function() { var index = i; deferreds.push(update({ success: function() { alert(index); } }));

Canceling a Deferred Promise in jQuery

心不动则不痛 提交于 2019-12-05 02:18:06
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. Good news. Since yesterday you can cancel your promise. I published the new version of my small plugin jquery-timing that provides two methods amongst

jquery: what happens when you pass a deferred object into “then”?

∥☆過路亽.° 提交于 2019-12-05 02:06:36
问题 I have been researching jquery deferred objects for a while now and I'm stumped on one thing. The "done," "always," "then," etc methods take, as their arguments, functions which should be called when the deferred object is resolved. However I tried chaining requests by passing a deferred object into the "always" method, and that seems to work too: // A is an array of data var req = ajax(url + "?d=" + A[0]).done(processResults).fail(reportFailure); for (var i = 1 ; i < A.length ; i++) { var

jquery deferred - “always” called at the first reject

风格不统一 提交于 2019-12-05 01:32:51
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(function(){console.log('then, done')}, function(){console.log('then, failed')}); var j = 0; var t =