I want to use my API asynchronously. I get some data from my API and i need to display them.
Here an example :
home.ts
async ngOnInit()
{
console.log("NgInit 01");
this.data = await this.api.getMe();
console.log("NgInit 02");
this.canDisplayData = true;
}
home.html
<div *ngif="canDisplayData">
myData : {{ data }}
</div>
service_api.ts
async getMe() {
let result: any;
await this.http.get('http://dummy.restapiexample.com/api/v1/employees')
.toPromise()
.then(res => { result = res; })
.catch(error => { console.log(error); });
return result;
}
this works, but i'm not proud at all of it. I'm sure i can do something better. Without use async and await.
Thank you for teaching me life, of angular/ionic :D