Home > Blockchain >  Call a method synchronously
Call a method synchronously

Time:04-05

I want to use a dialog to get the confirmation of a user. The dialog window works fine and the console.log drops the right value of the dialog. My question is how to handle it in the calling method. In line 2 you can see the subscription of the method. This is how I expected to do it but it drops the error "TS2339: Property 'subscribe' does not exist on type 'void'." I want to call the method synchronously, thats why I want to use the subscription. Do you have an idea how I can realize the method call correctly?

(I can't perform the action in the openDialog method because I want to reuse it for the other functions)

 deleteAll() {
    this.openDialog('Title', 'You really want to do it?').subscribe( result => {
      if(result === true) do something;
    });
  }


  private openDialog(title: string, message: string) {
    const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
      data: {
        message: message,
        buttonText: {
          ok: 'Ok',
          cancel: 'Cancel'
        }
      }
    });
    dialogRef.afterClosed().subscribe(result => {
      console.log(result);
      return result;
    });
  }

CodePudding user response:

You can just return the Observable from the afterClosed Method. If you always know it is going to be a boolean, you can chnage your openDialog function to return Observable

deleteAll() {
        this.openDialog('Title', 'You really want to do it?').subscribe( result => {
          if(result === true) do something;
        });
      }
    
    
      private openDialog(title: string, message: string):Obseravable<any> {
        return this.dialog.open(ConfirmationDialogComponent, {
          data: {
            message: message,
            buttonText: {
              ok: 'Ok',
              cancel: 'Cancel'
            }
          }
        });
        return dialogRef.afterClosed();
      }
  • Related