Reputation: 379
I am really new to angular, and wanted to understand how can i call async on Oninit. I have one service which is using asyn/await. i want to be able to use this in OnInit. however, I am not able to use it. because before service call is complete code executes to next line and it throws ZoneawarePromise error.
My Service method looks like below
public async getProjects():Promise<Observable<any>>{
const querySpec = {query: "SELECT * from c"};
const { resources:items } = await this.container.items.query(querySpec).fetchAll();
return of(items);
}
and this is how i am trying to call it within ngOnInit
async ngOnInit(){
(await this.projectData.getProjects()).subscribe(data=>{
//All the other code goes here, basically creating a HTML content
what could be the right way to call a service ?
Upvotes: 0
Views: 3343
Reputation: 19843
your problem is Promise of Observable
,
public async getProjects():Promise<Array<any>>{
const querySpec = {query: "SELECT * from c"};
const { resources:items } = await this.container.items.query(querySpec).fetchAll();
return items;
}
then
async ngOnInit(){
const data = await this.projectData.getProjects();
console.log('data',data);
Upvotes: 1