Struggling with implementing await
and async
in TypeScript, especially as a beginner. Below is how I attempted to use them:
async refreshList(){
await this.service.refreshList().subscribe(res => {
console.log(res);
this.service.todoListModel=res;
});
}
Through debugging and console output, I discovered that the subscribe method's code executes last. I need help fixing the component code below:
import { Component, OnInit } from '@angular/core';
// more imports...
@Component({
selector: 'todo',
templateUrl: './todo.component.html',
styleUrls: ['./todo.component.css'],
providers: [TodoService],
animations: [
// animations...
]
})
export class TodoComponent implements OnInit {
constructor(public service:TodoService) {
this.refreshList();
console.log(this.service.todoListModel);
}
// other methods...
public onTimeChange(t:any){
// method implementation...
}
addTodo(input: HTMLInputElement){
// method implementation...
}
removeTodo(i:number){
// method implementation...
}
}
Seeking assistance in resolving my issue. Any guidance on effectively using await
and async
would be greatly appreciated.