AngularJS 2 ZoneAwarePromise instead of boolean

I have a base class in AngularJS 2 for restClient to call data from an API using this method:

public getInfoDataBooleanRequest(url: string): InfoDataBoolean { return this.http.get(this.urlApiPrefix + url, this.createRequestOptions()) .toPromise() .then(response => <InfoDataBoolean>response.json()) .catch(this.handleError); } 

where InfoDataBoolean is a class with two properties:

 export class InfoDataBoolean { public data: boolean; public error: string; } 

I have another class where I named my service method. This call is inside a method in which I want to return data from an InfoDataBoolean, rather than an InfoDataBoolean class like this.

 public isLogged(): boolean { return this.getInfoDataBooleanRequest('islogged').then(x => { let result: InfoDataBoolean = x; if(result.error !== "1") { console.log('Success is failed'); return false; } return result.data; }); } 

The output of console.log(isLogged()) :

ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array [0]}

But I want to return true or false from my isLogged() method.

How can i do this?

+5
source share
1 answer

Remember that your isLogged method is asynchronous and returns a promise. To get the result, you need to register the callback using the then method:

 console.log(isLogged()); isLogged().then(data => { console.log(data); }); 

In your case, you show a promise and a return result when it is resolved ...

+9
source

All Articles