Subscription after switchMap does not work

late1

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.

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

martin

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Subscription for switchMap not called when observable takes more time

Continue the subscription after error

How does free limited digital subscription work without authentication?

How does switchMap() resolve a Promise?

Angular 4 losing subscription with router paramMap using switchMap

Angular6 rxjs unsubscribing to timer subscription OnDestroy does not work?

switchMap after catchError does not emit values

Does switchmap automatically calls the subscription?

No subscription notifications after successful subscription with Graph API

Start subscription after another subscription fires

Dispose after RX subscription

JavaScript does not work after outsourcing

Will MSDN product keys still work after the subscription is expired?

Meteor: Why does my subscription not work?

Xorg does not work after uninstallation of Nvidia drivers

Fstab after reboot does not work

Bootstrap popover does not work after first page

jquery - css does not work after .after()

Password does not work after lock

Will my Apple Wallet Pass generator work after my Apple Developer subscription is expired?

actioncable does not work after deploy to heroku

Subscription disposal -- does it wait for subscription action to end?

LAN connection does not work after upgrade to 18.04

Rxjs Subscription does not work in a Angular Directive

Why does Colspan not work after using jquery?

RxJs not switchMap if an error occured in the top level subscription

how can I do a switchMap and get the diferent responses in the subscription?

Angular subscription to switchMap() function does not wait for setInterval() and returns undefined

Swift: Does AsyncStream work in a subscription-based context?