How to return Observable after some Promise get resolved in Ionic 2/Angular 2?
With RXJS>6, fromPromise
is merged into from
function and to use mergeMap
, you need to use pipe
first. So this code will work
import { Observable, from } from "rxjs";
import { map, mergeMap } from "rxjs/operators";
logout(): Observable<any>{
return from(this.userData.getAuthToken()).pipe(mergeMap(token => {
this.token = token;
this.headers = new Headers({
"X-USER-TOKEN": token
});
this.options = new RequestOptions({
headers: this.headers
});
var logout_url = "Api logout method";
return this.http.post(logout_url, {}, this.options).map(res => res.json());
}));
}
Use fromPromise
to convert the promise into an observable and use mergeMap
to emit the HTTP response into the composed observable:
import { Observable } from 'rxjs/Observable/';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
logout(): Observable<any>{
return Observable.fromPromise(this.userData.getAuthToken()).mergeMap(token => {
this.token = token;
this.headers = new Headers({
"X-USER-TOKEN": token
});
this.options = new RequestOptions({
headers: this.headers
});
var logout_url = "Api logout method";
return this.http.post(logout_url, {}, this.options).map(res => res.json());
});
}