1
votes

I'm getting really confused and wondered if anyone can help me try to understand .subscribe in angular 2 rxjs.

If i have a page with a button to submit some form data, do i need to subscribe in order to post information or use a promise?

As my understanding is when you subscribe, then you click the button again, you will then have 2 instances of the subscribe and so on. Now i know what your going to say, well you can kill the subscription in the destroy or whatever, but that isn't my question.

My question is, if the form is never destroyed until you clicked the home button and the form was clearing upon submit in order to submit some more data, do I need to use Promise over Subscribe? As subscribe (from what I understand) is like a newsletter, the more you click subscribe, the more occurrences of the newsletter you will receive.

So which is best to submit data over and over again, without having multiple subscriptions?

myObsFunc(dataToSend).subscribe(err => console.log(err), () => console.log(success));

or

myObsFunc(dataToSend).toPromise().then().catch(e => console.log(e));

2
What is it that you are trying to achieve? What are you subscribing to on the button click? - Questioning
Well, It's true, but you can always disable the buttons until you get a response for the current request or something similar if you don't want to end up with multiple result sets. - cyberpirate92
For a form you can use ngSubmit like: <form [formGroup]="myForm" (ngSubmit)="myFunction()">. You don't need to subscribe or create a promise. - Farasi78
You need to use subscribe and unsubscribe only when you are handling Observables. - Shubham Agarwal Bhewanewala
Oh okay so your saying if i'm making a post request to the backend, i just need to call my subscription but not subscribe eg: myObsFunction(dataToSend), rather then myObsFunction(dataToSend).subscribe()? but then how do i check whether it was successful or not? - VVS

2 Answers

1
votes

Observables are lazy You could think of lazy observables as newsletters. For each subscriber a new newsletter is created. They are then only send to those people, and not to anyone else.

Observables can have multiple values over time Now if you keep that subscription to the newsletter open, you will get a new one every once and a while. The sender decides when you get it but all you have to do is just wait until it comes straight into your inbox.

If you come from the world of promises this is a key difference as promises always return only one value. Another thing is that observables are cancelable. If you don’t want your newsletter anymore, you unsubscribe. With promises this is different, you can’t cancel a promise. If the promise is handed to you, the process that will produce that promise’s resolution is already underway, and you generally don’t have access to prevent that promise’s resolution from executing.

0
votes

Observable is stream data. Hope this code will help you to understand that.

import {timer} from "rxjs/observable/timer";

let observable = timer(1, 1000); //timer will emit an event each second

let subscribtion = observable.subscribe((data) => {
  //do stuff for each tick of stream data
  console.log(data);
}, (reason) => {
  //when there was an error while watching observable
}, () => {
  //do stuff when stream is complete
});

//when you need to unsubscribe from this events you can call
subscribtion.unsubscribe();