Home > Enterprise >  How to make this function async?
How to make this function async?

Time:08-31

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') }})
  }

CodePudding user response:

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');

CodePudding user response:

firstValueFrom will return a promise with the first value sent by the Observable

firstValueFrom(this.timesheetService.getAllTimesheets()).then({next: (response: TimeSheet[]) => {this.timesheetsHandled = response.filter(sheet => sheet.status == 'HANDLED') }})
  • Related