-1

Problem to organize chain of calls by using rxjs observables with typescript

I am new in RXJS and have some trouble to organize chain of calls in my typescript code. Question is - how to make this.http.get('http://www.gooogle.com'); calling only once. This code is not real, just a minimum case for reproduce

import {Injectable} from '@angular/core';
import {flatMap, map} from 'rxjs/operators';
import {Observable, from} from 'rxjs';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class CoolService {

  constructor(private http: HttpClient) {}
  x(): Observable<Object> {
    return from(Promise.resolve({a: {b: 'c'}})).pipe(
      flatMap((x) => {
        console.log(x);
        return this.http.get('http://www.gooogle.com');
      }), map((res) => {
        console.log(res);
        return res;
      }));
  }
}


Alex Coder
  • 11
  • 2
  • Could you please include the imports that you are using to gain access for instance to `flatMap` and the other missing declarations. As is, the code you provided does not pass type checking. It would be particularly helpful if you were to provide a minimal, reproducible example. https://stackoverflow.com/help/minimal-reproducible-example – Shaun Luttin Jul 06 '19 at 20:28
  • @ShaunLuttin it's simple imports import {flatMap, map} from 'rxjs/operators'; – Alex Coder Jul 06 '19 at 20:30
  • Even with those imports, the code does not pass type check. – Shaun Luttin Jul 06 '19 at 20:39
  • @ShaunLuttin did another try, minimum case, full service code, thanks for commenting this – Alex Coder Jul 07 '19 at 08:02
  • 1
    What is your expected and realized behavior – bryan60 Jul 07 '19 at 08:05
  • @bryan60 okey, I have an template which is with |async tag , in real case I am doing uploading file to the s3 server, by using presigned URL , so first I'm calling my service which is using webRPC - and as answer I get Promise , based on this answer I want to filter out some data from it and then make simple http call for getting presigned url to post the file to s3 server. Then I want to upload to s3 by using presigned url and after- use s3api to get url for "get" this data . I use flatMap for chain - but it calling http requests a couple of times instead of one – Alex Coder Jul 07 '19 at 08:12
  • Post a complete minimal example reproducing the **actual** issue, tell precisely what you're doing, what you expect to happen, and what happens instead. – JB Nizet Jul 07 '19 at 08:15
  • Sounds like you’re accidentally subscribing more than once. The provided code does not recreate the issue you’re describing, so this is not a minimal reproducible example – bryan60 Jul 07 '19 at 08:15
  • Thx @bryan60 you were definitely right! – Alex Coder Jul 07 '19 at 08:27
  • Issue is unclear, and your quesiton does not provide the minimal code required to reproduce your problem. – Jota.Toledo Jul 07 '19 at 08:47

1 Answers1

0

The reason was my non-understanding of how rxjs operators works. All operators are doing work for each subscriptions, but I thought it's doing one calculations for all subscriptions. Because of that I did a chain of calls in service and subscribe on response in different place - as a reason, operators run as much as subscriptions in the code, so I need to rebuild this

Alex Coder
  • 11
  • 2