Passing through multiple parameters to rxjs operator Passing through multiple parameters to rxjs operator typescript typescript

Passing through multiple parameters to rxjs operator


Using forkJoin:

.pipe(  mergeMap((appVersion) => forkJoin(    of(appVersion),    this.storage.get('access_token').take(1)  )),  mergeMap(([version, token]) => {    this.createHeaders(version, token)  }))

But even easier with withLatestFrom:

.pipe(  withLatestFrom(this.storage.get('access_token')),  mergeMap(([version, token]) => {    this.createHeaders(version, token)  }))

I have included both because withLatestFrom is easier but forkJoin should be used where you need to access the initial parameter. For example, if you were doing this.storage.get(appVersion)


You may want to try something like this

private getHeaders(): Observable<any> {  return this.appInfo.getVersion()    .pipe(      mergeMap(version => this.storage.get('access_token')                          .map(token => ({version, token})),      map(data => this.createHeaders(data.version, data.token))    );}

ALTERNATIVE VERSION WITH ZIP

private getHeaders(): Observable<any> {  const versionObs = this.appInfo.getVersion();  const tokenObs = this.storage.get('access_token');  return Observable.zip(versionObs, tokenObs)    .pipe(      map(data => this.createHeaders(data[0], data[1]))    );}