.when() is not working with ajax call

.when() is not working with ajax call

Hi,


My code looks like this:
  1. $.when(myAjaxFunction()).done(function(myMessage){
  2.    alert(myMessage);
  3. });
In this case, I'm getting an error that myMessage is undefined. How can I make sure that the  myAjaxFunction() is executed and completed before I display myMessage. I thought .when() was designed for this scenario but it's not working.

Any idea why?

Thanks,
Sam