I'm currently facing an issue with my http.get call in which I need one of the parameters (getUserToken) to be returned before the function is executed. However, I don't want to chain them together since the calling function getSomeData returns an observable that is utilized by many other functions.
The problem lies in getSomeData as it requires getUserToken to be returned, but getUserToken always ends up being null due to improper handling of promises. I'm struggling to find a solution for this dilemma.
This function initiates the call and is used by multiple functions. I'm hesitant to make any changes here.
displayData(){
let mySubscription = this.getSomeData();
mySubscription.subscribe(retJson => {
if (retJson != null && retJson !== undefined){
console.log(retJson);
}
});
}
The issue arises when trying to return the http.get call due to the dependency on getUserToken.
getSomeData(){
let temporary = this.http.get(serviceUrl, getUserToken() ).map((response: Response) => {
return response;
});
return temporary;
}
The challenge lies in getUserToken making its own server call. Is there a way to encapsulate this within an observable or promise that can be resolved inline with the http.get inside the getSomeData function?
getUserToken(){
this.authenticationService.getToken()
.subscribe(responseData => {
if (responseData){
let headers = new Headers({ 'Authorization': 'Bearer ' + responseData.token });
return new RequestOptions({ headers: headers });
}
});
}