Reputation: 3308
From a simple jquery ajax call, I am returning a promise object to be handled by caller.
chkSession: function() {
if (session == undefined)
session = new Entities.Session({});
var deferred = $.Deferred();
/* if no session cookie, return false */
if (session.id === undefined) {
deferred.resolveWith(this, {result: -1});
return deferred.promise;
}
/* validate the session with server */
session.fetch({
complete: function() {
deferred.resolveWith(this, {result: this.result});
}
});
return deferred.promise;
}
somewhere else in the calling code...,
var promise = this.chkSession();
promise.done(function() { ... });
but the above code fails with error Uncaught TypeError: promise.done is not a function
If I try to print the 'promise' object in the caller context, it is not undefined, but points to a function:
What am I doing wrong here? I saw similar issue posted here as a ticket in jquery site, but this was resolved as works for me.
I tried using jquery 1.11.1 and 2.1.4. both versions show the same problem.
Upvotes: 1
Views: 9449
Reputation: 3308
credits to @roippi for pointing out the syntactical mistake.
It should have been
return deferred.promise();
instead of
return deferred.promise;
missing parantheses, after 'promise'.
Upvotes: 1