user1854438
user1854438

Reputation: 1842

Observables and Subscriptions

Can anyone explain to me Observables? They keep referring to observables as like promises. If I can call a method and return data, why do I need an observable?

Upvotes: 0

Views: 52

Answers (2)

Dan Nagle
Dan Nagle

Reputation: 5425

Promises sugar coat the callback pattern, do this then that then other. A Promise will act on data and either return a value or an error.

Since the creation of JavaScript, event listeners have been listening to and reacting to events in the browser. Observables are the latest and greatest abstraction of the observer pattern. It doesn't matter what the data source is, you can wrap an Observable around it.

When you are dealing with a stream of data, a Promise isn't of any use to you because the stream may not end, Observables solve the problem. Angular 2 uses Observables instead of Promises for dealing with HTTP.

Upvotes: 1

TyAnthoney
TyAnthoney

Reputation: 288

Angular is non blocking having promises/Observables allows your code to continue to run while the data you have requested is retrieved.

Upvotes: 1

Related Questions