11
Can anyone explain to me 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. Thank you!
11
Can anyone explain to me 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. Thank you!
8
Promises are native to the SE6 and are great options especially compared to callback, we can live well with them. Methods then(), that can help solve various problems, a good example would be to chain them like .then(...). then(...) . Methods such as All(), Race()... several good possibilities using only Promise. However, still on ES7 we have the Async/Await that only works with Promise!
To execute a Observable, it is necessary to use your method Subscribe(). Hi? Looks like the same then(), but it is not! While in Observable all programming can be performed with map(), catch() and all before being executed, and then when the Subscribe() Oh yes... oh yes, you will be executed. Promise, after the return what is done with the then() is treatment.
What I just said characterizes a Observable as Lazy (expensive), already Promise as ager (anxious). Do not confuse this word with Pacific Rim Jaegers! :
Promise will execute and use the then() to treat. The Observable no, he waits Subscribe() where it is who really executes and treats (reactive).
So it is a simple matter of preference? I believe not! But, for being reactive, Observable would be my choice! In your position as a function Retry(), that tries to reconnect or call your request again if you have connection oscillation problems, for example, is a great feature. Promise does not have this reactive characteristic, however Promise is already Native SE6, as stated at the beginning of this answer. We don’t need to import anything, Observable is an alternative already coming from Rxjs and needs to be imported, and very common to dev Angular 2+.
6
You may not be able to explain exactly the difference between these two concepts, but if you already have some help, take a look at the answer to the following link:
https://cursos.alura.com.br/forum/topico-observable-ou-promise-34392
By my understanding the Promise processes a single event when an asynchronous operation is completed or fails, whereas the Observable allows you to pass zero or more events where the callback is called for each event.
Oftentimes Observable is preferred because it provides the characteristics of Promise and more. With Observable no matter if you want to handle 0, 1 or multiple events. You can use the same API in each case. It also has the advantage of "being" a Promise cancellable .
I hope I’ve helped!
3
Observable By definition it is a collection that works one-way, IE, it sends notifications whenever a change occurs in one of your items and from this we can perform an action.
But what are the advantages of using Observable and not Promises? The big advantage is the "powers" that Observable gives us with its operators, for example: We can "cancel" requests to save processing, or even try to re-request if any problems like connection loss happen. User does not need to see that error screen.
1
One of the differences is that using observable you can return multiple values while in Promises you return only one, example: https://youtu.be/3rbikUwGf9Y
Another difference is that to use observable You need to import Rxjs that allows you to use operators such as map, filter and others, as if you were dealing with an array, but does not allow you to use async/await. Already in Promises you don’t need to import anything to use it and you can use async/await.
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
Hello Z, take the content of the Link and replicate it here, because if one day this link breaks the answer will still be valid.
– KaduAmaral