Johan
Johan

Reputation: 35194

$.Deferred notify() & progress() synchronous confusion

I'm using a deferred object that I sometimes want to notify synchronous (if I already have my result in the cache).

Why does it only receive the last notification?

var dfd = $.Deferred();

for(var i = 0; i < 3; i++){
    dfd.notify(i);
}

dfd.progress(function(i){
    console.log(i); // 2
    // expected: 0, 1, 2
});

http://jsfiddle.net/uShAP/

Upvotes: 1

Views: 52

Answers (1)

gontrollez
gontrollez

Reputation: 6538

You only receive one notification because the progress is set after calling notify. If you set the progress method first you'll receive all:

var dfd = $.Deferred();

dfd.progress(function(i){
    console.log(i); 
});

for(var i = 0; i < 3; i++){
    dfd.notify(i);
}

This happens because when you set the progress method, and notify has been already called, you'll probably want to receive the last value only, so you can reflect the current status inmediately.

Upvotes: 1

Related Questions