Search code examples
angulartypescriptasync-awaitservice

How to make this function async?


How do I turn the below function in a async function?

  getHandledSheet(): void {
    this.timesheetService.getAllTimesheets().subscribe({next: (response: TimeSheet[]) => {this.timesheetsHandled = response.filter(sheet => sheet.status == 'HANDLED') }})
  }

Solution

  • As in Matthieu's answer but fixing the syntax errors:

    const timesheetObservable = this.timesheetService.getAllTimesheets();
    const timesheets = await firstValueFrom(timesheetObservable);
    this.timesheetsHandled = timesheets.filter(sheet => sheet.status == 'HANDLED');