2

I need to wait for this observable to return (simplified):

assembleOptions(options?: RequestOptionsArgs): Observable<boolean> {

    let auth = this._cookieService.get(this.auth);

    if (auth === undefined || auth === null) {
        this._http.get('/someValue').map(x => {
            //do something with x
            return Observable.of(true);
        })
    } else {
        return Observable.of(true);
    }
}

This appears to return an Observable<true> which doesn't really matter, as I am not using it, I just need to wait for it, then I try to use it within this:

public get(url: string, options?: RequestOptionsArgs): Observable<Response> {
  return this.assembleOptions(options).map(x => {
      return this._http.get(url, this.options);
  });
}

Where this._http is from

import { Http, Response, Headers, RequestOptionsArgs } from '@angular/http';

Which does not work, it is returning an Observable<Observable<Response>>

Type 'Observable<Observable<Response>> is not assignable to type 'Observable<Response>'

What am I missing here? I have tried casting all over the place to no avail.

0

1 Answer 1

2

You want switchMap instead of map

return this.assembleOptions(options).switchMap(x => {
      return this._http.get(url, this.options);
  });

From the switchMap documentation

Map to observable, complete previous inner observable, emit values

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this! Exactly what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.