Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subscription after switchMap does not work

I have created a Confirmation dialog that returns true or false, based on the user’s choice.

This is the function that opens the dialog and handles the responses:

public action(id: number): void {
    this.dialogService.openDialog(ConfirmationComponent).afterClosed()
        .pipe(
            switchMap(
                (result: boolean) => {
                    if (result) {
                        return this.service.doSomething(id);
                    } else {
                        return of('cancelled');
                    }
                }
            )
        ).subscribe({
            next: (response: string) => {
                this.snackBarService.openSnackBar(response, 'valid');
            },
            error: (error: HttpErrorResponse) => {
                this.httpService.handleError(error);
            },
            complete: () => {
                this.getData();
            }
        }
    );
}

When the dialog returns false, the snack-bar opens with the message cancelled (as expected) and the getData() function gets executed. However, when the dialog returns true, the doSomething(id) function gets executed (as expected), but the snack-bar does not show up, and getData() is not executed.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does anybody have any idea why it’s behaving like that? I can’t figure it out.

>Solution :

This really depends on what this.service.doSomething(id) returns. I suppose it’s an Observable but based on your description it never emits any next and no complete. It might return an empty Observable such as EMPTY or just new Observable().

So the solution here is to make sure that this.service.doSomething(id); emits and completes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading