You must pass your callbacks as functions:
$.when(foo1())
.done(function() {
alert('success');
}).fail(function() {
alert('fail');
});
Currently, you're attempting to register the undefined
result of immediately calling alert('fail')
as your .fail
handler.
Although you didn't say so, you will be seeing both the success
and fail
alerts appearing, which should provide a hint that you did something wrong ;-)