What is the difference between Promises and Observables?

7

Can anyone explain the difference between Promises and Observables? I would like to understand the advantages and disadvantages of each. Which is more performative, which applies a best practice for development and etc. Thanks!

    
asked by anonymous 25.05.2017 / 04:59

3 answers

3

You may not be able to explain exactly the difference between these two concepts, but if it is already a bit of help, take a look at the answer below:

link

Promise strong> allows you to pass zero or more events where the callback is called for each event.

Often Observable is preferred because it provides the characteristics of Promise and more. With Observable it does not matter whether you want to manipulate 0, 1, or multiple events. You can use the same API in each case. It also has the advantage of being a Promise

I hope I have helped!

    
25.05.2017 / 14:30
3

Promises are native to ES6 and are great options mostly compared to callback, we can live well with them. The then () methods, which can help solve various problems, would be a good example to chain them into .then (...). Then (...) . Methods like All () , Race () ... several good possibilities using only Promise. However, ES7 we still have Async / Await that only works with Promise!

To run an Observable , you must use your Subscribe () method. Hi? It looks like then () , but it is not! While in Observable you can perform all the programming with map () , catch () and everything before it is executed, and then when fire the Subscribe () then yes ... yes, it will run. Promise , after the feedback what is done with then () is treatment.

What I just said features an Observable as Lazy , already Promise as eager (anxious). Do not confuse that word with Jaegers of Pacific Rim! : o P

Promise will run and use then () to handle. But the Observable does not, it expects Subscribe () where it is who actually executes and treats (reactive).

So it's a simple matter of preference? I do not think so! But, being reactive, Observable would be my choice! In your possibilities as the Retry () function, which tries to reconnect or call your request again in case you have connection oscillation problems, for example, is a great resource. Promise does not have this reactive feature, but Promise is already ES6 as said at the beginning of this response. We do not need to import anything, Observable is an alternative already RxJS and needs to be imported, and very common for dev Angular 2 + . p>     

08.12.2018 / 12:21
2

Observable By definition it is a collection that works in a unidirectional way, that is, it issues notifications whenever a change occurs in one of its items and from that we can perform an action.

But what are the advantages of using Observable and not Promises? The great advantage is in the "powers" that Observable gives us with its operators, for example: We can "cancel" requests to save processing, or even try to make a new request in case some problem like connection loss happens. The user does not need to see that error screen.

source

    
27.01.2018 / 20:05