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
10
votes
2 answers

Promise callback not called in Angular JS

If I call connect from doStuff, I get the message that "the socket is connected", but the callback is not called. What am I missing here? $scope.connect = function() { var defer = $q.defer(); ws = new WebSocket("ws://server.com:port"); …
dndr
  • 2,319
  • 5
  • 18
  • 28
10
votes
4 answers

jquery deferred - wait until two calls complete

I am looking for a way to do a callback after two ajax calls completes: $.when( call1(), call2() ).always(function() { // Here I want to be sure the two calls are done and to get their responses ); The catch is that one of the calls…
Naor
  • 23,465
  • 48
  • 152
  • 268
10
votes
2 answers

jquery deferred - "always" called at the first reject

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 ;…
zazabe
  • 178
  • 2
  • 7
9
votes
2 answers

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

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…
Fabio
  • 18,856
  • 9
  • 82
  • 114
9
votes
2 answers

jQuery Promise then not working after AJAX

I have my Promise defined as so: myFunc = function() { $.getJSON("./rest/api/some/url", function(json, textStatus) { console.log("AJAX call hit!"); }); }; $.when(myFunc()).then(function() { console.log("Then block…
user1757703
  • 2,925
  • 6
  • 41
  • 62
9
votes
1 answer

How convert Angular promise to jquery deferred object

I want to return promises from my module/sdk to non-angular javascript. For example if I'm returning promise to a jquery, I should be probably sending jquery deferred object. How can I convert an Angular promise to a jquery promise/deferred obj. Any…
phani
  • 1,134
  • 1
  • 11
  • 24
9
votes
2 answers

JQuery deferred reject immediately

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…
Niclas
  • 217
  • 3
  • 9
9
votes
3 answers

How to transfer all the handlers from one deferred to another?

Let's say I have a $.Deferred and a jqXHR object. Is there a way to transfer all the handlers bound to the deferred (then, always, done, fail) over to the XHR object (which, as I understand it, is an extension of Deferred)? Here's what I had in…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
9
votes
2 answers

how can I return values from chained deferreds in jQuery?

I'm trying to trace the return value from a function call: $('#button').on('click', function(){ console.log( getMessage(3) ); // I'm trying to get this to "hang" until ajax-related stuff is finished below }); The ajaxFetch() below is a generic…
tim
  • 3,823
  • 5
  • 34
  • 39
9
votes
1 answer

jQuery Deferred .then() isn't called after .fail()

I'm using jQuery.Deferred and registering the done, fail and then handlers: $.when( some_ajax(url) ) .done(function(result){}) .fail(function(){}) .then(function(){}); //just like that, with a single parameter I've found that when my…
Haji
  • 1,715
  • 7
  • 25
  • 41
9
votes
3 answers

Use jquery done on "non-ajax" function

Can I use jquery done() on "non-ajax" functions. I get the error Uncaught TypeError: Cannot call method 'done' of undefined when I try to do something like this. function countThreeSeconds() { var counter = 0, timer = setInterval(function…
1252748
  • 14,597
  • 32
  • 109
  • 229
9
votes
2 answers

jQuery promise with getJSON and callback

I have an ajax call with a callback. I want to call another method JUST after the callback has ended..I used the promise API from jQuery but as you can see below the second method is called before the first one has completed. Any ideas? my.data =…
Guy Z
  • 683
  • 3
  • 8
  • 24
8
votes
1 answer

Can someone explain clearly how jQuery.when() and deferred.then() works?

I'm working on a web application and I need to load a few files $.ajax. I found something interesting in $.when().then(). It works great when I don't have anything special to do with the data returned by the request like this example: $.when( …
Gabriel
  • 2,720
  • 4
  • 28
  • 36
8
votes
3 answers

Dealing with Arrays of Deferred Objects

Since using $.Deferred I've run into this scenario a couple times: I have a list of values each of which yields a Deferred Object in some way and I want to execute a callback once all of the Deferred Objects are resolved. A more concrete example…
8
votes
4 answers

jquery deferred turn failure into success

So in using jQuery deferreds and $.when to load many objects in parallel. $.when( a.ajax(), b.ajax(), c.ajax() ).then( //do something when all are complete complete(); ); Now, b.ajax() will sometimes fail, but I dont actually care. I just…
dalyons
  • 1,304
  • 3
  • 15
  • 23