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
25
votes
1 answer

jQuery Deferred: Rejecting a Promise from within a Done Filter

Fyi, I'm just starting to learn jQuery promises, so I may be a bit confused here. Anyway, I have an AJAX request that I want to reject from within a done filter based on the content of the response: return doAJAXRequest().then(function (data) { …
Ajedi32
  • 45,670
  • 22
  • 127
  • 172
24
votes
4 answers

jQuery Deferred's, $.when() and the fail() callback arguments

I'm getting an unexpected result when using $.when() when one of the deferred operations does not succeed. Take this JavaScript, which created 2 deferreds. The first one succeeds and the second one fails. var f1 = function() { return…
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
24
votes
3 answers

When should I reject a promise?

I'm writing some JS code that uses promises. For example, I open a form pop-up and I return a jQuery Deferred object. It works like this: If the user clicks OK on the form, and it validates, the Deferred resolves to an object representing the form…
cdmckay
  • 31,832
  • 25
  • 83
  • 114
23
votes
2 answers

How to use jQuery Deferred with custom events?

I have two abstracted processes (e.g. managed within js objects using the revealing module pattern that do not expose their internals) that fire custom events when they complete. I want to perform an action when both custom events have fired. The…
Adam Flynn
  • 949
  • 2
  • 9
  • 21
22
votes
5 answers

Using jQuery load with promises

I'm still trying to wrap my head around deferred and what not, so with this in mind I have a question on how to do the following. My team and I have 3 separate .load() methods that each go grab a specific template and append that to the same…
Mike Fielden
  • 10,055
  • 14
  • 59
  • 99
22
votes
3 answers

Return an empty promise

I have a function that returns a jQuery promise. It looks like this: addBooks(books: Array) { return $.ajax({ url: '/Books/AddBooks/', type: 'POST', data: ko.toJSON(books), contentType: 'application/json' …
Jonathan Eckman
  • 2,071
  • 3
  • 23
  • 49
22
votes
6 answers

Asynchronous Loop of jQuery Deferreds (promises)

I am trying to create what I think is referred to as a "Waterfall". I want to sequentially process an array of async functions (jQuery promises). Here's a contrived example: function doTask(taskNum){ var dfd = $.Deferred(), time =…
Jon Wells
  • 4,191
  • 9
  • 40
  • 69
20
votes
1 answer

jQuery $.when() with variable arguments

I want to send [1, n) AJAX-requests to the server and after all have returned a result a modal dialog should be closed. $.when(a(), b(), c()) would be perfect, but I don't know how to pass the variable count of functions to $.when as parameter. Any…
Botic
  • 203
  • 2
  • 5
20
votes
3 answers

in JavaScript, how to wrap a promise in timeout?

It's a common pattern to implement timeout of some asynchronous function, using deffered/promise: // Create a Deferred and return its Promise function timeout(funct, args, time) { var dfd = new jQuery.Deferred(); // execute asynchronous…
mnowotka
  • 16,430
  • 18
  • 88
  • 134
19
votes
2 answers

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

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…
camwest
  • 333
  • 1
  • 3
  • 6
19
votes
3 answers

Understanding Deferred.pipe()

I've been reading about deferreds and promises in jQuery but I haven't used it yet. I've understood everything very well but the method pipe. I really didn't get what it is. Could some please help me to understand what it does and where could it be…
Diego
  • 16,436
  • 26
  • 84
  • 136
18
votes
5 answers

Waiting for jQuery AJAX response(s)

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…
Olive
  • 3,516
  • 7
  • 24
  • 32
18
votes
3 answers

jQuery deferred object with nested ajax calls

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…
Aesthete
  • 18,622
  • 6
  • 36
  • 45
17
votes
2 answers

jQuery Deferred vs jqXHR

I use: var jqXHR = $.ajax(settings); jqXHR.success(function(result){}); jqXHR.error(function(result){}); jqXHR.complete(function(result){}); But version 1.5 has added the deferred object. Q: In general, when do you use success, error and complete…
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373
16
votes
1 answer

JQuery Deferred. Using $.when and .progress()

I am writing a parser littered with Async Tasks. I use JQuery promises to control and order the async tasks. This is a psuedo code version of the constructor function: /** * @constructor */ function Parser(json) { …
Jon Wells
  • 4,191
  • 9
  • 40
  • 69
1 2
3
76 77