jquery-deferred

Force jQuery Deferred to wait until Ajax complete in “then” handler

蹲街弑〆低调 提交于 2019-12-02 07:51:27
I have situation where I believe I need to create a Deferred object with a "then" handler, but wait until the "then" handler has completed it's own promise before moving on. The use case is a record object, and the above function is it's save method. The record object has an attribute called saveQueue, which is set to $.Deferred() on the record's instantiation. The resolve call on saveQueue was supposed to make sure the Deferred there is always executing every new handler attached to it as soon as it could. The idea being that you can call save several times on the record in short succession,

jQuery deferred ajax cache

安稳与你 提交于 2019-12-02 07:35:34
I read the top answer to this question regarding the use of jQuery Deferred . I'm looping through an array of IDs. For each ID, I need to get data pertaining to it either from an ajax request, or from a cache if an ajax request had already successfully returned the data before. During each loop, I use a $.when() to observe whether getData() returns something from cache or a successful ajax call, before processing that ID. The current problem is that the ID processing proceeds anyways without waiting for getData()'s ajax to succeed. Some pseudocode: var IDs = ["1", "2", "1", "3", "1"]; //ID "1"

Is there a way to continue after one deferred fails?

…衆ロ難τιáo~ 提交于 2019-12-02 04:51:52
问题 I'm handling an unknown number of ajax requests. The request could fail in a 404. This causes the whole chain to fail. Is there a way to continue after one deferred fails? var deferreds = []; // fill deferreds with a number of ajax requests. $.when.apply($, deferreds) .done(function(){ // handle done }).fail(function(){ // handle fail // would like to fix/resolve the failed deferred and continue with the rest }); 回答1: You have to create your own deferred object, waiting for the other deferred

syntax for Jquery.deferred , making synchronous function return promise

强颜欢笑 提交于 2019-12-02 01:38:45
A quick question on how to use Jquery.deferred to make a slow synchronous function return a promise instead. What I've done so far is this : function sayIt(ms) { setTimeout( function() { console.log('what I say'); }, ms); } function doIt() { return $.Deferred( function() { sayIt(2000); }).promise(); } doIt().then( function() { console.log('ah'); }); the sayIt(2000) always goes through but the chained function after the 'then' never fires. If I do this : doIt().then( console.log('ah')); the 'ah' comes up right away, and then the 'what I say' 2000ms later - what I want is of course the opposite

Is there a way to continue after one deferred fails?

99封情书 提交于 2019-12-02 01:26:29
I'm handling an unknown number of ajax requests. The request could fail in a 404. This causes the whole chain to fail. Is there a way to continue after one deferred fails? var deferreds = []; // fill deferreds with a number of ajax requests. $.when.apply($, deferreds) .done(function(){ // handle done }).fail(function(){ // handle fail // would like to fix/resolve the failed deferred and continue with the rest }); You have to create your own deferred object, waiting for the other deferred to succeed or fail. var myDeferred = $.Deferred(); var origDeferred = $.ajax(...); // if request is ok, i

Wait for multiple promises to be rejected

孤街醉人 提交于 2019-12-02 00:34:32
问题 Doing some testing, I need to wait for a couple of promises to be rejected. I know that I can use jQuery.when() to wait for several promises tu be resolved. But that method reject the master promise as soon one of the promises fails. I know that all my promises will fail, but I need to wait for it anyway. How can I do it? In a kind of pseudo-code what I want to do is: var promise1 = connection.doCall(); var promise2 = connection.doCall(); $.when([promise1, promise2]).allOfThemFail(function()

Can I pass Promises to jQuery.when(), or only Deferreds?

家住魔仙堡 提交于 2019-12-01 21:05:31
The documentation for jQuery.when() says that this function takes Deferreds. However, it also says later on that: If a single argument is passed to jQuery.when() and it is not a Deferred or a Promise... which seems to imply that it can take Promises as well. But Promises are not Deferreds - they have a subset of the Deferred's methods. I guess you could say that a Deferred is a Promise, but a Promise is not a Deferred. Questions: Can $.when() take either Promises or Deferreds? This seems to work in my testing. Is there a bug in the doc? I think it should say that $.when() takes Promises, not

JQuery deferred reject immediately

倖福魔咒の 提交于 2019-12-01 15:57:25
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 reject on the same thread? dfd.reject(asyncResult); return dfd.promise(); } $.get('/api/testapi/get')

Why I can't pass console.log as a callback argument in Chrome (and Safari)?

我的未来我决定 提交于 2019-12-01 15:48:24
The following snippet will produce an error in Chrome (and Safari) while it works in Firefox. I'd expect to have 2 numbers shown in javascript console, but in Chrome I only get the first and then an Uncaught TypeError: Illegal invocation // a generic promise that return a random float var makePromise = function() { return $.Deferred().resolve(Math.random()); } // This works in all browsers makePromise().then(function(d) { console.log(d); }); // This works in firefox only makePromise().then(console.log); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Why I can't pass console.log as a callback argument in Chrome (and Safari)?

与世无争的帅哥 提交于 2019-12-01 14:59:06
问题 The following snippet will produce an error in Chrome (and Safari) while it works in Firefox. I'd expect to have 2 numbers shown in javascript console, but in Chrome I only get the first and then an Uncaught TypeError: Illegal invocation // a generic promise that return a random float var makePromise = function() { return $.Deferred().resolve(Math.random()); } // This works in all browsers makePromise().then(function(d) { console.log(d); }); // This works in firefox only makePromise().then