Cancel subscribe

Asked

Viewed 44 times

-1

I’m performing a call to API that returns me a list of any obj when I click on a button. That is if I click twice in sequence, the API returns me the same list in sequence as well.

My question is, how do I make it if I click twice fast in sequence, I cancel the first request and only pick the value of the last click. I tried to use Switmap, but I was not successful. Follow the code below...

getBookingByUserAndStatus(): void
{
        this.loading = true;
        const getByUserAndStatus = this._bookingsService.getByUserAndStatus(this.user._id, this.statusSelect).subscribe(resp => 
        {
            console.log(resp);
            this.loading = false;
            this._foverNotificationService.notify('Lista carregada', SnackbarStatusEnum.success);
        },
        error =>
        {
            this.loading = false;
            this._foverNotificationService.notify(error, SnackbarStatusEnum.error);
        },
        () => getByUserAndStatus.unsubscribe());
}

inserir a descrição da imagem aqui

If I click 2 times in sequence on the test button, I get 2 times in sequence the API response as shown in the notification..

Thanks in advance....

1 answer

0

Try to use the take(1).

It would look something like this:

this._bookingsService.getByUserAndStatus(this.user._id, this.statusSelect)
        .pipe(take(1))
        .subscribe(resp => ... );
  • It also didn’t work, I edited my question, putting visually what happens!!

  • Try without the getByUserAndStatus.unsubscribe().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.