What is the difference between observable and promises?

devquora
devquora

Posted On: Feb 22, 2018

 

    1 Answer Written

  •  devquora
    Answered by Satyam

    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.

Related Questions

Please Login or Register to leave a response.

Related Questions

Angular 2 Interview Questions

What is Angular 2 ?

Angular 2 is a completely revived component-based framework in which an application is a tree of loosely coupled compon..

Angular 2 Interview Questions

List some advantages of Angular 2 over Angular1.

Angular 2 is a re-written version of Angular1 and not an update. The best way to compare Angular 2 and Angular 1 is by f..

Angular 2 Interview Questions

What are the new features of Angular 2?

Angular 2 is a platform that encompasses a wide range of capabilities. Some new features were added in Angular 2 which i..