TypeScript – wait for an observable/promise to finish, and return observable

The problem is that we convert observable into different type… with .subscribe – while we should not (it does not return observable)

public makeRequest = (): Observable<any> => {
    return this.myObservable().subscribe(
      ... // this is wrong, we cannot return .subscribe
          // because it consumes observable and returns ISusbcriber
    );
}

When we have an observable… we should just take its result and use .map to convert it to something else

FlatMap operator

transform the items emitted by an Observable into Observables, then
flatten the emissions from those into a single Observable

public makeRequest = (): Observable<any> => {
    return this.myObservable()
       .flatmap((x) => return this.http
              .get('http://jsonplaceholder.typicode.com/posts/1')
              .map( (responseData) => {
                    return responseData.json();
              })
              ...

Check all the details here

TAKING ADVANTAGE OF OBSERVABLES IN ANGULAR 2

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)