Retrieving Data using HTTP Flashcards

1
Q

What is observable?

A

A collection of items over time.

Unlike an array, it doesn’t retain items. Emitted items can be observed over time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does an observable do?

A

Nothing, until we subscribe. When we subscribe the observable emits notifications.
If everything is okay and item is emitted, next notification is emitted.
If an error occurs observable emits error notification. If there are no more items to emit, observable emits a complete notification.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Common observable usage

A
  1. Start the observable (subscribe)
  2. Pipe emitted items through a set of operators
  3. Process notifications: next. error, complete
  4. Stop observable (unsubscribe)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Variables that reference an observable

A

Should have ‘$’ as suffix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

HttpClientModule needs to be included

A

in the imports array of the app module

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Imports array of @NgModule is used for declaring

A

external modules

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Declarations array of @NgModule is used for

A

declaring components, directives and pipes that belong to that module

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

HTTP get method returns an observable so

A

we will receive a notification. Which means
getProducts(): Observable {
return this.http.get(this.productUrl);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To handle exceptions of observable we need

A

tap operator- taps intor observable stream without transforming the stream,
catchError -catches the error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Subscribing to an observable

A
x.subscribe()
or
x.subscribe(Observer)
or
x.subscribe({ nextFn, erroFn, completeFn})

Subscribe returns subscription. We can use it later on to cancel subscription

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Unsubscribing from an Observable

A

It’s a good thing to always unsubscribe.

  1. Store the subscription in a variable
  2. Implement the OnDestroy lifecycle hook
  3. Use the subscription variable to unsubscribe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly