0

I'm getting to know hyperledger composer angular apps. This piece of code that yeoman generator produces puzzle me. The add method executes return this.http.post and it returns a response object, that is chained to a map operator to convert to json output. But all references I could find to map operator show that its argument must be specified, like an arrow function (v)=> {some instructions to perform on v here});, or as an anonymous function (f(v){some instructions here});

I've seem the code below also in tutorials as a best practice to deal with response objects. Obviously map operator also accepts a function and it sends the resulting response object to whom is chained to as the argument of the `map function. Could anyone provide links to where this mechanism is explained?

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';


public add(ns: string, asset: Type): Observable<Type> {
    console.log('Entered DataService add');
    console.log('Add ' + ns);
    console.log('asset', asset);
    return this.http.post(this.actionUrl + ns, asset)
            .map(this.extractData)
            .catch(this.handleError);
}


private extractData(res: Response): any {
    return res.json();
}
Milo
  • 3,365
  • 9
  • 30
  • 44
Olegario
  • 1
  • 3
  • may be your are looking for the `map` operator in Rxjs https://www.learnrxjs.io/operators/transformation/map.html – Niladri Jun 01 '18 at 17:18
  • Here is an answer https://stackoverflow.com/questions/28107986/what-does-the-map-method-mean-in-rxjs – Niladri Jun 01 '18 at 17:18
  • BTW `res.json();` is not required anymore in the new `HttpClient` of angular. it internally calls `res.json();` . So your call would become `return res;` if you are using `HttpClient` instead of `Http` – Niladri Jun 01 '18 at 17:21
  • @Niladri I've seem that, but as you can see, syntax of map operator is as I said signature: map(project: Function, thisArg: any): Observable. So in my example, where is the thisArg??, even the examples cited there show the anonymous and arrow functions with the argument on it. Same for the answer you posted, it features anonymous function but coulnd't find answer to it. Please feel free if I missed something – Olegario Jun 01 '18 at 17:53
  • Ah, yes the Client I'm using is deprecated but as I said, it's yeoman generated and they haven't update the tool yet. I'll consider it when wrinting my original code. thanks, – Olegario Jun 01 '18 at 17:58
  • I've found an example that makes it my point clearer: var squarefunc = function(x) { return x*x; }; values = [1,2,3,4] values.map(squarefunc) // returns [1,4,9,16] Here squarefunc receives the value of values array. Something similar must be going on with the chained response object produced. So I think then it's not a feature of new observable map operator object but a former map instruction feature? – Olegario Jun 01 '18 at 17:59
  • The `map` operator for Rxjs observables works in the same way as the map in javascript array. – Niladri Jun 02 '18 at 14:16

0 Answers0