0

I have multiple deferred objects. I need to attach handlers to be fired once per deferred object.

I know that I can use

$.when.apply($, my_array);

but as I understand it, done will only be fired once all deferred objects are resolved.

I need done to be fired every time an object is resolved. I can probably use a loop but I would like to know if there is something like on the lines of the above line using $.when.

2 Answers 2

1

You've got to use a loop; there's no syntax sugar for handing each promise individually.

function doneCallback() {
  // your common callback
}

$.each(my_array, function(my_deferred) {
    my_deferred.done(doneCallback);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Not exactly sure if this is what you're trying to do but it sounds like what you need to do is create a master deferred object then pipe the array of deferred objects to it. Add your handlers to the objects in the array using .always() which fires whether the deferred fails or succeeds. When all of the child deferred objects are resolved the master deferred .done() will fire.

Also $.when() can handle arrays of deferred objects so you could try $.when(my_array).always(function()...)

3 Comments

I think OP wants the done handler to fire for each promise. (eg 3 promises results in 3 done calls.)
In that case I think he can do my_array.on('done', function(){});
@Mathletics is right.Is on('done') officially supported. I couldn't find any info on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.