Interviews5 answers
Differentiate between Observables and Promises.
5 Answers
An Observable is like a Stream (in many languages) and allows to pass zero or more events where the callback is called for each event. Often Observable is preferred over Promise because it provides the features of Promise and more. With Observable it doesn't matter if you want to handle 0, 1, or multiple events.
Abhijit SahooPosted Jul 4, 2018, 2:56 AM EDT
Promises:
return a single value
not cancellable
more readable code with try/catch and async/await
Observables:
work with multiple values over time
cancellable
support map, filter, reduce and similar operators
use Reactive Extensions (RxJS)
an array whose items arrive asynchronously over time
Shivam ShuklaPosted Jun 27, 2018, 4:53 AM EDT
hello deepak, A Promise handles a single event when an async operation completes or fails.
Note: There are Promise libraries out there that support cancellation, but ES6 Promise doesn’t so far.
An Observable is like a Stream (in many languages) and allows to pass zero or more events where the callback is called for each event.
Appsierra SolutionsPosted Jun 26, 2018, 1:09 AM EDT
Hi Deepak,
Both we are using for getting the response . The main difference is :
1-
A Promise handles a single event when an async operation completes or fails while
An Observable is like a Stream and allows to pass zero or more events where the callback is called for each event.
2- Observable is preferred over Promise because observable is cancelable while promise can not .
3- Observable support map, filter, reduce and similar operators .
4- With the Observable we are using Rxjs which is the great thing in angular because Rxjs is a library for reactive programming using observables that makes it easier to compose asynchronous or callback-based code.
Hope Its help you .
Bhairab DuttPosted Jun 25, 2018, 10:07 PM EDT
Promise :
A Promise handles a single event when an async operation completes or fails.
Observable:
An Observable is like a Stream of data that allows to pass zero or more events where the callback is called for each event.
Often Observable is preferred over Promise because it provides the features of Promise and more. With Observable it doesn't matter if you want to handle one or multiple events. You can utilize the same API in each case.
Observable also has the advantage over Promise to be cancelable. If the result of an HTTP request to a server or some other expensive async operation isn't needed anymore, the Subscription of an Observable allows to cancel the subscription, while a Promise will eventually call the success or failed callback even when you don't need the notification or the result it provides anymore.
Observable provides operators like map, forEach, reduce, ... similar to an array
Ashish RanjanPosted Jun 25, 2018, 9:45 PM EDT




