Mohammad Hossein Amri
Mohammad Hossein Amri

Reputation: 2015

how to carry forward a value that was available before switchmap?

I have an rxjs that pass an id, and I need to call an api, the api will return some data and I need to process that data regards to the projectId that wasa available before the switchMap.

if I map it to {projectId, api: this.http.get('/api/projects/'+projectId)} will not work like switchmap.

selectedProject$.pipe(
    switchMap(projectId=> this.http.get('/api/projects/'+projectId)), 
    map(data=> this.createChart(
                    projectId, /* I need the project id that was available before the switchMap */
                        data))
)

I know I can set tap(projectId=> this.selectedProjectId=projectId) and use it later, but I want to know a way to handle it with rxjs

Upvotes: 0

Views: 960

Answers (1)

Fan Cheung
Fan Cheung

Reputation: 11360

Looks like you want to pass projectId down the chain, you can chain map after http.get to extract that data for downstream

switchMap(projectId=> this.http.get('/api/projects/'+projectId).pipe(map(data=> ({data,projectId})),
 map(({data,projectId})=>.... 

Upvotes: 2

Related Questions