Life is About Weathering The Storms

Life is complicated. Life is like a roller coaster. Life is a combination of the good and the bad (the yin and the yang). Whichever cliche you enjoy dishing out when waxing poetically about this…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Understanding Observables in Angular

You could create a whole course about them. Here is a brief overview.

An Observable is like a data source. In an Angular project, it’s an object we import from a third-party package, rxjs. The observable here is implemented by following an observable pattern where we have an observable and an observer and in between, we have a timeline where we can have multiple events, or data packages, emitted by the observable (depending on the data source of the observable). These events can be triggered programmatically, by user interaction, like a button, HTTP requests, and dozens of other data sources.

We have three ways of handling these packages. We can handle data, errors, or the completion of the observable, because these are the three types of data packages you can receive. It’s in these three that your code gets executed.

What should happen if I receive a data package? What should happen if there is an error or when the observable is completed?

Some observables are never done, like a button that can be clicked a bunch of times, others will resolve with the event (like an HTTP request). Generally, though, they are all used to handle async tasks. All these data sources triggered in your code are async tasks, you don’t know when they will happen or how long they will take and you don’t want to wait until the completion of these events blocking your other code. Historically you would use callbacks or promises, observables are just a different approach that is often the preferred way in Angular. Their major advantage is its operators, which we will talk more about later.

Observables are constructs to which you subscribe to see changes in data. Params is a good example:

Our params (of type Params) is a stream of route parameters that gives us a new…

Add a comment

Related posts:

Thinking About Thinking

I enjoyed learning about the study that was done with thinking about thinking. It made me look at my own life and see if I have used thinking about thinking in school. I believe I have used this…

Advantages of Studying in Canada Over Other Countries

Thousands of international students come to Canada every year making it one of the most popular destinations for both graduate and post-graduate education. Let us see some of the reasons why students…

What school are you at and what year are you in?

Prior to entering law school I worked at Goodmans LLP as a legal assistant and studied at Humber College where I obtained a Bachelor of Applied Arts in Paralegal Studies. My favourite part about…