So I have read up about Observables and have even used it in an application I created. So this is my understanding of an Observable- that it is a wrapper around a continuous stream of data either synchronous or asynchronous or both. So basically it can return multiple values. Also there is an observer associated(subscribed) to the observable. Whenever the observable execution receives a new value the observer receives it. So I had two questions-
If you want to return multiple values, why dont we create separate promises for each of those requests, and simply put them in promise.all, so when all of them resolve, we have what we need, instead of making multiple requests in an observable.
Our use of either of them, depends upon our use case , if we are not sure when, if and how many values we are going to receive , thats when observable are most useful. So for example- if we are listening to a connection , where we are receiving live commentary of a football game and then displaying it in our application. Here we would not know how many responses we would receive.
I know it was a very big question but i had to state everything i understand right now, so if there is anything which is not clear , someone would clear it for me.
Thanks in Advance.
Ok I might be wrong here but they are of completely different purposes. this is my understanding of difference between them.
Considering point one, and becuase of point2 if the promise source keeps growing we will never reach a resolution state since we can only resolve once in promises, which is why observables are good here. They will react whenever there is a change is stream
var a = async(b + c);
var d = wait for a;
what is d?
in promises to know d we just need b +c. we know it will happen but only we don't know when so we just wait. it doesn't matter how many are there in chain it will eventually (resolve). even for chains
var a = async(b+c) + async (c+d)
var d = promise.all(a)
this is promise.all wait for everything but after it happens(resolve) there is no point for waiting for promises afterwards.since we don't care what happens to a.
var a = 1 + 1 for every second;
var d = a + 1;
what is d?
here we will never know what value of d it is forever changing. we can only read current value (observe) a and give get current value of d.
var a = 1 + 1 for every second;
var d = observe(a) + 1;
but we cant use promise.all if we use this in this scenario
var a = 1 + 1 for every second;
var d = promise.all(a) + 1; //wont work
there is always change. so we have to wait permanently. And that is the difference between promise.all and observables. sorry for the long post.