1

Question: Is the approach below a good way of doing it in Angular with RxJS using observables .pipe .map or.subscribe?

Scenario: After getting the data from a http call, I want to use this data in a seperate (long & heavy) transformation step and then return both - the original data (e.g. for tables) and the transformed data (e.g. for charts).

First I used nested subscriptions and then found this blog post exlaining how to avoid this. I tried to apply it to my scenario but not sure if this is the best way doing it?

// this would be in my service
fetchData(): Observable<any> {
    return this.http.post<any>(this.url, this.body, this.options)
        .pipe(
            switchMap(response => {
                // Received response object
                const dataOrig = response;

                return this.createChartData(response)
                    // Combine the dataOrig object and the resolved dataChart to a single object
                    .pipe(
                        map(dataChart => ({ dataOrig, dataChart }))
                    );
            }));
};

createChartData(data: any) {
    // do some heavy transformation with data here
    let output = ['a', 100, 'b', 50];   // example output
    return from(output);                // not sure if I need an observable here at all
}



////////////////////////////////////////
// this would go later in the component
final = {
    dataOrig: null,
    dataChart: null
};

finalStep() {
    this.fetchData()
        .subscribe(({ dataOrig, chartData }) => {
            // Update the view with the matching information
            this.final.dataOrig = dataOrig;
            this.final.dataChart = chartData;
        });
}
2
  • 1
    I would suggest , do all the data manupulation at component level . This will also give you the flexibity to keep the actual data returned from the API as remains in unchanged state/ Commented Feb 19, 2021 at 5:57
  • So would you call the data-tranform function from inside the .subscribe to the api call in the component? But would you then not end up with nested .subscribe as the data-transform also should be an observable? Commented Feb 19, 2021 at 8:36

1 Answer 1

3

In general my advice is to put as much logic as possible in the services, including data transformation. The main reason for me is that it makes testing much easier.

In your case it looks like the POST returns some data that you need to transform in your app. Such transformation seems synchronous, i.e. it does not require any call to any external async resource, so I do not see why you should need switchMap. A simple map should do the work.

So, at the end, what I would do is more or less this

fetchData(): Observable<any> {
  return this.http.post<any>(this.url, this.body, this.options)
    .pipe(
       map(response => {
         const transformedData = this.createChartData(response);
         return { response, transformedData }
       })
     )
}

createChartData(data: any) {
    // do some heavy transformation with data here
    // you do not have to return an Observable here
    return resultOfTransformation
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.