As a newcomer to TypeScript & RxJS, my goal is to return an Observable
once another Observable
has completed:
public myObservable = () : Observable<boolean> => {
console.log('Retrieving the token from the database');
return Observable.create(observer => {
setTimeout(() => {
observer.next(true);
observer.complete();
}, 5000);
});
}
public makeRequest = (): Observable<any> => {
return this.myObservable().subscribe(
function (x) {
console.log('Token acquired, proceeding with HTTP call');
return this.http.get('http://jsonplaceholder.typicode.com/posts/1')
.map( (responseData) => {
return responseData.json();
})
.map((item:any) => {
return {
id: item.id,
userId: item.userId,
title: item.title,
body: item.body
};
});
},
function (err) {
console.error('Error: ' + err);
},
function () {
console.log('Process completed');
});
}
Encountered this error message: "Returned expression type subscription is not assignable to type Observable<any>
".
While I grasp the concept of this error (an Observable as a stream, and a subscription as the act of observing that stream), I'm uncertain about how to "await" the completion of an Observable
(or a promise) before returning a new Observable
. Any suggestions on how to approach this?