Is there a more elegant way of doing the following?
private getHeaders(): Observable<any> {
let version;
let token;
return this.appInfo.getVersion()
.pipe(
tap(appVersion => version = appVersion),
mergeMap(() => this.storage.get('access_token')),
tap(accessToken => token = accessToken),
mergeMap(accessToken => of(this.createHeaders(version, token)))
);
}
How can I more fluently remember the two return values of this.appInfo.getVersion()
and this.storage.get('access_token')
without writing them to temporary variables with the power of rxjs?
Perhaps merging some observables into one? There are so many rxjs operators and stuff...
Using
forkJoin
:But even easier with
withLatestFrom
:I have included both because
withLatestFrom
is easier butforkJoin
should be used where you need to access the initial parameter. For example, if you were doingthis.storage.get(appVersion)
Use forkJoin, it's designed for this type of situation.
You may want to try something like this
ALTERNATIVE VERSION WITH ZIP