Get and subscribe

When you return to the code, we can see that we use the HttpClient class by calling two consecutive methods: .get<Quiz[]>() and .subscribe(). The former, as the name suggests, issues a standard HTTP request to our .NET Core QuizController to fetch an array of quizzes; we use a local string variable to assemble the controller's endpoint URL and then toss it as a parameter. The latter instantiates an Observable object that will execute two very different actions right after a result and/or in case of an error. Needless to say, all this will be done asynchronously, meaning that it will run in a separate thread (or scheduled for later execution), while the rest of the code continues to execute.

It's very important to understand that we're only scratching the surface of what an Observable can do. However, this is all we need for now: we'll have the chance to talk more about them later on.

Observables are a powerful feature for managing async data; they are the backbone of the ReactiveX JavaSript Library (RxJS), which is one of the Angular required dependencies, and are planned to be included in the final release of EcmaScript 7. If you're familiar with ES6 Promises, you can think of them as an improved version of that approach.

Angular makes extensive use of both Observables when dealing with data. Those who want to get additional information can take a look at the following URL, taken from the RxJS official documentation:

http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.80.100