angularrxjshttpclientconcatmap

Angular 12 ConcatMap how i can do it?


I want to concat the tow api call. How i can use ConcatMap on this code ?

    getHIndices(code) {

        
        this.api.getInstrumentHistoryIndices(code, '3M')
            .subscribe((response: {}) => {
                this.prepareDataForHistory(response);
            });

        setTimeout(() => {
            this.api.getInstrumentHistoryIndices(code, '5Y')
            .subscribe((response: {}) => {
                this.prepareDataForHistory2(response);
            });
        }, 300);

    }

Solution

  • Try the following:

    getHIndices(code) {
      this.api
        .getInstrumentHistoryIndices(code, '3M')
        .pipe(
          tap(response1 => {
            this.prepareDataForHistory(response1);
          }),
          concatMap(() => this.api.getInstrumentHistoryIndices(code, '5Y'))
        )
        .subscribe(response2 => {
          this.prepareDataForHistory2(response2);
        });
    }