Reputation: 802
I started to work deeply with promises, and need your help to understand why this code doesn't work as expected.
Here is what I need:
After successful profile retrieving the code should log out ('success getting Profile!') which it does, and return true
as result, that should be passed to the next then()
element. But instead of true
, I get undefined
as the result.
public verifyAuth(): angular.IPromise<boolean> {
let promise: ng.IPromise<boolean> = this._Token.getIdToken()
.then((idToken) => {
if (!idToken) {
return false;
}
else if (this._Profile.isEmpty()) {
promise = this.retrieveProfileInfo()
.then(() => {
this._$log.log('success getting Profile!');
return true;
});
} else {
return true;
}
})
.catch((error) => {
this._Token.clearToken();
this._$log.error(error);
return false;
});
return promise;
}
Upvotes: 1
Views: 118
Reputation: 193261
You are returning the first promise
object before your reassign it later in the success then
block. You don't need to do it just use chaining like this:
public verifyAuth(): angular.IPromise<boolean> {
let promise: ng.IPromise<boolean> = this._Token.getIdToken()
.then((idToken) => {
if (!idToken) {
return false;
}
else if (this._Profile.isEmpty()) {
return this.retrieveProfileInfo()
.then(() => {
this._$log.log('success getting Profile!');
return true;
});
} else {
return true;
}
})
.catch((error) => {
this._Token.clearToken();
this._$log.error(error);
return false;
});
return promise;
}
Note, how you simply return this.retrieveProfileInfo()
which becomes new promise
automatically.
Upvotes: 2