jquery-deferred

JQuery deferred reject immediately

本小妞迷上赌 提交于 2019-12-01 14:45:40
问题 When using JQuery.Deferred is it OK to invoke reject() directly? Without having invoked a async function? Perhaps I want some kind of test in the beginning of my async function. If the test fails I want to reject immediately. See the first if block below. function doSomethingAsync() { //Test if the ajax call should be invoked var testFailed = true; var dfd = $.Deferred(); //Check if test failed if (testFailed) { var asyncResult = { success: false, data: 'test failed' }; //Is this OK usage of

Wait for the first of multiple jQuery Deferreds to be resolved?

安稳与你 提交于 2019-12-01 13:46:59
With jQuery I know that I can use $.when() to wait for all of multipe Deferred s to be resolved. (Or for the first one to be rejected.) But is there a simple way to fire of multiple Deferred s and then just wait for the first one to be resolved? For instance I want to try to use two similar AJAX web services either or which might be down or slow and then process whichever one replies first. And then I might use a third Deferred for a timeout. Based on Kevin B's code, here's an approach that uses a master Deferred object: var masterDeferred = new $.Deferred(), reqOne = $.post("foo.php"), reqTwo

How to call deferred functions sequentially?

让人想犯罪 __ 提交于 2019-12-01 13:02:31
My code: <?php if(isset($_GET['m'])) { $m = $_GET['m']; sleep($m); print "done, m=$m"; die; } ?> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script> <script> function w(s) { document.body.innerHTML = document.body.innerHTML+ "<br>" + s } function aaa(def) { w("begin aaa"); $.ajax({ type: "GET", data: { m: 5 } }).done(function(html) { w(html); def.resolve(); }); } function bbb(def) { w("begin bbb"); $.ajax({ type: "GET", data: { m: 1 } }).done(function(html) { w(html); def.resolve(); }); } $(function() { $.when( $.Deferred(function(d) { aaa(d) }).promise(), $

Wait for the first of multiple jQuery Deferreds to be resolved?

限于喜欢 提交于 2019-12-01 12:47:15
问题 With jQuery I know that I can use $.when() to wait for all of multipe Deferred s to be resolved. (Or for the first one to be rejected.) But is there a simple way to fire of multiple Deferred s and then just wait for the first one to be resolved? For instance I want to try to use two similar AJAX web services either or which might be down or slow and then process whichever one replies first. And then I might use a third Deferred for a timeout. 回答1: Based on Kevin B's code, here's an approach

Jquery Deferred in each loop with ajax callback

我与影子孤独终老i 提交于 2019-12-01 12:33:25
With reference to https://stackoverflow.com/a/13951699/929894 , I tried using deferred object in nested ajax loop. However the output is not returning as expected. I have updated my code in fiddle for reference. - https://jsfiddle.net/fewtalks/nvbp26sx/1/ . CODE: function main() { var def = $.Deferred(); var requests = []; for (var i = 0; i < 2; i++) { requests.push(test(i)); } $.when.apply($, requests).then(function() { def.resolve(); }); return def.promise(); } function test(x){ var def = $.Deferred(); test1(x).done(function(){ setTimeout(function(){ console.log('processed test item', x);

Jquery Deferred in each loop with ajax callback

拥有回忆 提交于 2019-12-01 11:54:04
问题 With reference to https://stackoverflow.com/a/13951699/929894, I tried using deferred object in nested ajax loop. However the output is not returning as expected. I have updated my code in fiddle for reference. - https://jsfiddle.net/fewtalks/nvbp26sx/1/. CODE: function main() { var def = $.Deferred(); var requests = []; for (var i = 0; i < 2; i++) { requests.push(test(i)); } $.when.apply($, requests).then(function() { def.resolve(); }); return def.promise(); } function test(x){ var def = $

use jquery deferreds for variable number of ajax requests

我的未来我决定 提交于 2019-12-01 10:44:41
when I have a variable number of ajax requests, how can I call them using deferreds? my guess: //qty_of_gets = 3; function getHTML(productID, qty_of_gets){ var dfd = $.Deferred(), i = 0, c = 0; //this is where there could be some magic to //do multiple ajax posts //obviously I'm out of my depth here... while (i <= qty_of_gets){ dfd.pipe(function(){ $.get("queries/html/" + product_id + i + ".php"); }); i++ } dfd.done(function(){ while (c <= qty_of_gets){ $('myDiv').append(c); c++; } }); } Felix Kling If you want to execute the Ajax calls sequentially, you have to return the promise from the

How to call deferred functions sequentially?

怎甘沉沦 提交于 2019-12-01 10:05:32
问题 My code: <?php if(isset($_GET['m'])) { $m = $_GET['m']; sleep($m); print "done, m=$m"; die; } ?> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script> <script> function w(s) { document.body.innerHTML = document.body.innerHTML+ "<br>" + s } function aaa(def) { w("begin aaa"); $.ajax({ type: "GET", data: { m: 5 } }).done(function(html) { w(html); def.resolve(); }); } function bbb(def) { w("begin bbb"); $.ajax({ type: "GET", data: { m: 1 } }).done(function(html) {

jQuery $.wait() for *all* deferred's to be rejected?

孤者浪人 提交于 2019-12-01 09:33:52
jQuery has a nice feature of its Deferred API, $.wait() for working with multiple Deferred s / Promise s. It returns when: All of the Deferred s have been resolve() d or One of the Deferred s has been reject() ed Most of the time this is what you want, but sometimes you want to know when all of them have been reject() ed. Is there a simple or elegant way to do something like $.wait() but only when all Deferred s have been rejected? (There may be other use cases but mine is to combine this with waiting for the first of several Deferred's to resolve .) In the spirit of how the Promise

Loop with each iteration only happening after jQuery deferred when/then possible without recursion?

北城以北 提交于 2019-12-01 06:50:24
I want to call jQuery deferred functions in a loop but each iteration should wait for the previous iteration to complete using the deferred when() function (num_of_iterations) { var arr = []; for (var i = 1; i < num_of_iterations; ++i) { arr.push($.getJSON( ... 1 ... )); arr.push($.getJSON( ... 2 ... )); ... $.when.apply($, arr).then(function() { // somehow do the next iter. only now that all the asynch getJSON's are done }); } return; } Now of course since getJSON is asynchronous all the requests in all iterations will actually be sent before any of the when s are invoked. I realize I can