James Earlywine James Earlywine - 2 months ago 16
Javascript Question

jQuery deferred.notify not working

I want to use the jQuery Promise Library/API. I wrote tidbit of code to test/learn it, but I'm not getting the expected results.

I paste this code into my javascript console:


window.dfd = $.Deferred();

$.when(window.dfd.promise()).then(
function(status) {console.log('resolved, status: ', status);},
function(status) {console.log('rejected, status: ', status);},
function(status) {console.log('notified, status: ', status);}
);


At this point, I would expect

window.dfd.notify('hello');


to trigger the 3rd callback and log to console - but it doesn't.

These both give the expected result:

window.dfd.resolve('hello');
window.dfd.reject('hello');


Does anyone know why I'm not getting the expected notify result? - and/or what I can do differently to get the expected result?

Answer

It works fine for me James, maybe it is the order you are calling resolve, reject or notify.

Once you resolve or reject a deferred, you can't notify it anymore.

If you try your code as it is and do something like:

window.dfd.notify('hello');
window.dfd.resolve('hello');

You should see the expected income.

Once you resolve or reject, you don't see notifications anymore so:

window.dfd.notify('hello');
window.dfd.resolve('hello');
window.dfd.notify('hello'); //this won't log anything