Search code examples
angularrxjsobservableangular-httpclient

How to generate the content of an observable based on the content coming from another observable


We have a function returning an Observable<bool> to indicate the success or failure of an operation.

public operation(parameter: Parameter): Observable<boolean> {
    ... generate dataToSend with parameter values and other information  ...
    this._httpClient.post<AType>(this._url, dataToSend).subscribe(data => { this.processResult(data); });
}

The function processResult returns a boolean but I don't know how to put the returned value inside the Observable<boolean> that I want to return from operation()

I guess I will have to create a new Observable to return it but I don't get how to execute the observer.next with the data coming from this.processResult(data). Something like this:

public operation(parameter: Parameter): Observable<boolean> {
    ... generate dataToSend with parameter values and other information  ...
    this._httpClient.post<AType>(this._url, dataToSend).subscribe(data => { this.processResult(data); });
    return Observable.create(observer => observer.next('**the value coming from this.processResult(data)**'));
}

Solution

  • Instead of doing the subscribe in here use map... Map will keep your result as an observable.

    return this._httpClient.post<AType>(this._url, dataToSend)
       .map(data => { return this.processResult(data) })
    

    be sure to put 'return' before your http post is made