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

Does switchmap automatically calls the subscription?

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

switchMap after catchError does not emit values

Rxjs Subscription does not work in a Angular Directive

Meteor: Why does my subscription not work?

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

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

How does free limited digital subscription work without authentication?

Angular6 rxjs unsubscribing to timer subscription OnDestroy does not work?

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

JavaScript does not work after outsourcing

Fstab after reboot does not work

Password does not work after lock

jquery - css does not work after .after()

How does switchMap() resolve a Promise?

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

Subscription for switchMap not called when observable takes more time

Angular 4 losing subscription with router paramMap using switchMap

Continue the subscription after error

Dispose after RX subscription

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

Start subscription after another subscription fires

No subscription notifications after successful subscription with Graph API

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

Bootstrap popover does not work after first page

LAN connection does not work after upgrade to 18.04

actioncable does not work after deploy to heroku

Why does Colspan not work after using jquery?

Xorg does not work after uninstallation of Nvidia drivers