Home Angular 4/5 > httpclientmodule .get() chaining functions and return-data types
Reply: 2

Angular 4/5 > httpclientmodule .get() chaining functions and return-data types

theMobDog
1#
theMobDog Published in 2018-01-11 05:40:30Z

Please see both answers. (Thanks JLRishe and AngularInDepth.com)

AngularInDepth had also added interesting link to interceptors and HttpClient mechanics in his post:

https://blog.angularindepth.com/insiders-guide-into-interceptors-and-httpclient-mechanics-in-angular-103fbdb397bf


Using RxJS and HttpClientModule, When calling .get() of HttpClient what are the return values in this chain?

constructor (private _http: HttpClient) {
...
this._http.get(url).map(res=>res.json()).toPromise()   // how does this chaining work?

As I understand, _http.get(url) returns an observable, but we are chaining a .map() to .get() method.

I am also guessing that the following .map() work as receiving http-response object, which we promptly converted to json object. Finally, the whole observable is converted to promise with .toPromise() which doesn't seem to care whatever .map() is doing to the .get()

  1. So far I understand that _http.get() returns an observable and will also provide http-response object (as next or callback function)
  2. is .map() function acting just on the response object and will not change the return type of the whole _http.get() function. (return type remains the same as observable)? Is my assumption correct?
  3. In general, if the above is true, how does JS figure out what kind of chaining function would work on the callback data and which would work on the return type? How do we implement my own custom function so that my chain function would strictly work on the callback data (correct me if I am using the right term) OR the return type?
  4. Can we add .map() to all async functions (promise,observables,callbacks) to work on their callback data? Likewise, does .toPromise() work on all async function to convert them to promises?
JLRishe
2#
JLRishe Reply to 2018-01-11 06:14:12Z
  1. So far I understand that _http.get() returns an observable and will also provide http-response object (as next or callback function)

_http.get() returns an observable of http-response objects. You can think of an observable as a window on a stream of values, and in this case that stream would be just a single value (the result)

  1. is .map() function acting just on the response object and will not change the return type of the whole _http.get() function.

Yes. It creates a new observable in which the values from the original observable have been transformed in some way. In this case, that transformation is res => res.json().

  1. In general, if the above is true, how does JS figure out what kind of chaining function would work on the callback data and which would work on the return type? How do we implement my own custom function so that my chain function would strictly work on the callback data (correct me if I am using the right term) OR the return type?

I don't really understand this question. The function passed to map needs to take the observable's contained type as input, and produce some type as output.

  1. Can we add .map() to all async functions (promise,observables,callbacks) to work on their callback data? Likewise, does .toPromise() work on all async function to convert them to promises?

You can use .map() on any observable and you can use .toPromise() on any observable. Bear in mind that .toPromise() creates a promises for the first value that comes out of the observable, so it's typically suited for observables that produce a single value (as this one does).

AngularInDepth.com
3#
AngularInDepth.com Reply to 2018-01-11 06:29:20Z

So far I understand that _http.get() returns an observable and will also provide http-response object (as next or callback function

The signature for the request method (which is what is called by get internally) is :

request(...): Observable<any>

which means that it returns an observable that emits items of type any. One of the values it returns is HttpResponse. However, depending on the observe parameter it can return text as in response.body or parsed JSON as in JSON.parse(response.body).

is .map() function acting just on the response object and will not change the return type of the whole _http.get() function. (return type remains the same as observable)? Is my assumption correct?

map operates on the values emitted by the observable. And the function signature doesn't explicitly specify these types and instead uses a generic type any.

Can we add .map() to all async functions (promise,observables,callbacks)

Here map is specific to the RxJs library. It's not map from the Array.prototype.map.

In general, if the above is true, how does JS figure out what kind of chaining function would work on the callback data and which would work on the return type?

You need to read about RxJs operators and lettable operators here. RxJs will execute each operator one after the other. All operators work on the data emitted by an observable, in the case of HttpClient it's either HttpResponse, response.body or JSON.

You can read more about how observable chain works in HttpClient in the article:

  • Insider’s guide into interceptors and HttpClient mechanics in Angular
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308751 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO