python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Promise vs. Observable - Angular 5 and 4

Promise vs. Observable - Angular 5 and 4

What is the difference between observables and promises on Angular 5?
What Is Promise?
Promise handles a single event when an asynchronous (async) operation completes or fails.
It helps us to run functions asynchronously and return only one value (or exception) when executed.
Promise is not Lazy.

What Is Observable?
Observable is like a Stream and allows us to pass zero or more events where the callback is called for each events.

Observable provides operators like map, forEach, reduce and similar to an array.
Observable is preferred over Promise because it provides the features of Promise and more.

Promise -
1.      Returns a single value
2.      Not cancellable - It can be Reject or Resolve
3.      More readable code with try/catch and async/await
4.      Promise is not Lazy.

Observable -
1.      Works with multiple values over time
2.      Cancellable
3.      By default, it is Lazy as it emits values when time progresses.
4.      It is cancel-able/retry-able and supports operators such as map, filter, reduce etc.
5.      Use Reactive Extensions (RxJS)
6.      An array whose items arrive asynchronously over time

Both Promises and Observables will help us work with the asynchronous functionalities in JavaScript applications.

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions