Home  >  Q&A  >  body text

What is the difference between RxJS Complete vs Add, which one is better for loading spinners?

I have this code below, I have done and added at the same time, and I have been using add as the method to stop loading the spinner when I call the controller, as that seems to be the correct way to stop loading the spinner (if There is some problem with the call getting from the controller because Add() is always called.

But I want to know what is the purpose of complete and should I use it instead of add to prevent my spinner from rotating client side? What is the difference between add and complete?

this.loadingSpinner = true;

this.membersService.getMemberProfile().subscribe({
  next: (v) => {
    // load profile into form
  },
  error: (e) => {
    console.error(e);
  },
  complete: () => {
    this.loadingSpinner = false;
  }
}).add(() => {
  this.loadingSpinner = false;
});

P粉649990273P粉649990273400 days ago750

reply all(1)I'll reply

  • P粉127901279

    P粉1279012792023-09-16 14:52:06

    Observable.subscribe returns a Subscription object, and Subscription.add< /a> is a way to tell a subscription to perform certain actions when unsubscribed.

    Observer.complete Called to listen for successful completion when the observable is called.

    So for your code, .add() works better because it will be called whether there is an error or it completes successfully.

    reply
    0
  • Cancelreply