En mi aplicación, tengo un botón que permite a los usuarios calcular algunos datos cuando se hace clic en él. Para hacer el cálculo, se llama a un servicio desde el back-end. Una vez que el servicio finaliza el cálculo, se muestra un mensaje que dice "Terminado". Pero el mensaje aparece mucho antes de que el servicio finalice el cálculo. Aquí está mi código, ¿cómo puedo solucionarlo?
sendCalculateMaterialPlanDetail() { this.confirmDialogRef = this._dialog.open(FuseConfirmDialogComponent, { disableClose: false, }); this.confirmDialogRef.componentInstance.confirmMessage = "Will start to calculate."; this.confirmDialogRef.afterClosed().subscribe((result) => { if (result) { this._productionService .sendCalculateMaterialPlanDetail(this.materialPlan) .subscribe((response: IMaterialPlan) => { this.materialPlan = response; }); } this._messages.Show( "DONE", "DONE", 3 ); }); }sendCalculateMaterialPlanDetail() { this.confirmDialogRef = this._dialog.open(FuseConfirmDialogComponent, { disableClose: false, }); this.confirmDialogRef.componentInstance.confirmMessage = "Will start to calculate."; this.confirmDialogRef.afterClosed().subscribe((result) => { if (result) { this._productionService .sendCalculateMaterialPlanDetail(this.materialPlan) .subscribe((response: IMaterialPlan) => { this.materialPlan = response; this._messages.Show( "DONE", "DONE", 3 ); }); } }); }muestra el texto dentro de la suscripción interna (controlador de resultados del servicio)
Puede usar las palabras clave await y async
async sendCalculateMaterialPlanDetail() { this.confirmDialogRef = this._dialog.open(FuseConfirmDialogComponent, { disableClose: false, }); this.confirmDialogRef.componentInstance.confirmMessage = "Will start to calculate."; this.confirmDialogRef.afterClosed().subscribe((result) => { if (result) { /////////////////////////////////////////////////here await this._productionService .sendCalculateMaterialPlanDetail(this.materialPlan) .subscribe((response: IMaterialPlan) => { this.materialPlan = response; }); } this._messages.Show( "DONE", "DONE", 3 ); }); }