Questions tagged [jquery-deferred]

jQuery's deferred (also known as promises and futures) can be used to manage callback queues

jQuery.Deferred(), introduced in version 1.5, is a chainable utility object that can register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.

1147 questions
12
votes
2 answers

jquery deferred and return false based on server response

I have the below jquery deferred logic. var $qCallA = callA(); var $qCallB = callB(); $.when($qCallA,$qCallB).then(function () { $("#spinnerDiv").removeClass('spinner show'); }); function callA() { return $.getJSON("/callA", function…
JavaUser
  • 25,542
  • 46
  • 113
  • 139
12
votes
2 answers

jQuery, $.ajax with array of urls

I have a simple array of urls, and I want to load each one with jQuery. I was using $.get, but I cannot seem to get it to work with $.Deferred, so I switched to $.ajax - I almost have it working, but the results I am getting are .. odd. I was hoping…
Ciel
  • 4,290
  • 8
  • 51
  • 110
12
votes
8 answers

Check Internet connectivity with jquery

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…
Gaurav
  • 1,005
  • 3
  • 14
  • 33
12
votes
1 answer

not asynchronous function executed as jQuery Deferred

Lets say I want to process some tasks in the synchronous manner, so I have this function: function executePromiseQueueSync(queue){ var seed = $.Deferred(), finalPromise; finalPromise = _.reduce(queue, function(memo, promise){ …
mnowotka
  • 16,430
  • 18
  • 88
  • 134
12
votes
2 answers

Jquery ajax done callback not responding to 201

I have an ajax post as such: $.post("/api/v1/payment_methods/create_credit_card", values) .done (response) -> console.log("GOOD JOB") .fail (response) -> console.log("Adas") The response is a 201, however, done doesn't seem to be capturing it…
JustNeph
  • 761
  • 3
  • 10
  • 25
12
votes
2 answers

JQuery deferred changes "this"

I'm attempting to have a javascript object run a deferred method and when it's .done() call a function in that same object. I'm having issues because "this" becomes the deferred object instead of the object that called…
Chris
  • 1,418
  • 2
  • 16
  • 34
11
votes
4 answers

Canceling a Deferred Promise in jQuery

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…
ripper234
  • 222,824
  • 274
  • 634
  • 905
11
votes
2 answers

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

Please note This is a contrived example. function longFunc(){ var deferred = $.Deferred(); setTimeout(function(){ console.log("long func completed"); deferred.resolve("hello"); }, 3000); …
Vikram
  • 4,162
  • 8
  • 43
  • 65
11
votes
3 answers

Clear/Remove attached handlers to $.Deferred Objects

Very simple question, thought I could figure this one out but the answer has eluded me. Is it possible to clear/remove attached handlers to a deferred object done/fail/always queues. var dfd = $.Deferred() dfd.done(foo).fail(bar); // User…
Frzy
  • 339
  • 3
  • 5
11
votes
3 answers

Implement Deferred object without using jquery

I want to implement basic Deferred object without using jQuery. Here i will be implementing only done and fail callbacks, with resolve and reject functions. and ofCourse associating promise method with this function. i am doing the following…
codeofnode
  • 18,169
  • 29
  • 85
  • 142
11
votes
2 answers

jQuery Deferred with an array of functions

I have an object full of functions like so: var functions = { fun1 : function(){ ... } fun2 : function(){ ... } fun3 : function(){ ... } }; The object keys are all referenced inside an array like so: var funList =…
ValZho
  • 351
  • 2
  • 11
10
votes
1 answer

How do I use jQuery .when() function with a dynamic set of ajax calls?

I'm using JqGrid and for each row in the grid I'm loading, I am making an ajax call to get additional data. Once that's all complete, I need to apply some formatting. I would like to use $.when(), but I'm not sure how to call it. I was researching…
IronicMuffin
  • 4,182
  • 12
  • 47
  • 90
10
votes
3 answers

jQuery when/then/fail with concurrent ajax requests: Which request failed?

Imagine a scenario where we want to do something after the concurrent requests for 'foo' and 'bar' have completed successfully, or report an error if one or both of them fails: $.when($.getJSON('foo'), $.getJSON('bar')) .then(function(foo, bar) { …
Javier
  • 101
  • 1
  • 1
  • 4
10
votes
3 answers

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

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); }); …
Omer Bokhari
  • 57,458
  • 12
  • 44
  • 58
10
votes
3 answers

$.Deferred vs new $.Deferred

What is the difference between var dfd = new $.Deferred and var dfd = $.Deferred In which cases you need to use new vs not using it?
mayrop
  • 681
  • 9
  • 19