What is the difference between observable and promises?

  • Sharad Jaiswal
  • 22nd Feb, 2018

1 answer(s) :

  •   Reply


    18th Oct, 2019

    Difference between Observable and Promise:

    Observable: With the help of observables a user can asynchronously run functions. Their return values can be used in a continuous sequence(multiple times) when it is executed. It is Lazy by default because only with the progression of time it emits its values. Observables have many operators and that makes the coding process very easy or in other words, it simplifies the efforts that developers put into the coding.

    The operator 'retry' can be used when the user wants to retry something, whenever they wish to. If someone wants to retry the observable that is based on some conditions then the operator "retryWhen" can be used. Some more operators that we can use under observables are Map, forEach, filter and reduce. An observer is nothing but a function that returns “function Observer: An object with next, error” when it takes the input of as an “observer”.

    Promises: A task that will finish in the future can be called a “Promise”. With the help of promises a user can asynchronously run functions. Their values(or exceptions) can be returned but only once when it is executed. This is not the lazy one like the observer. Cancellation option is not available, there may be some promise libraries that have the option of cancellation but the ES6 promises don’t support cancellation as of now, the options that can be used instead of cancellation are "Reject" and "Resolve".

    One more drawback of the promises is that it cannot be retrieved, promises need access to the original function that earlier returned the promise so it can use the retry option, but, giving it access to the original function is not a good practice. Unlike the observers, there are no operators in promises. One more different characteristic is that it can call the services without the .then and .catch.

Post your Answer :

Valid name is required.

Valid name is required.

Valid email id is required.