I hope there is no answer to this question, I couldn't find anything helpful for my case. I'm kinda new to angular and typescript so: how can I extract the value returned by a subscription when calling a rest call? The code looks like this:
static utilAccountName(....., queryService: QueryService) {
...
let value = true;
let name;
do {
....
// the name is built and should be changed here until there shouldn't be in the backend
value = this.theValueExists(name: string, queryService: QueryService);
} while (value);
return name;
}
private static theValueExists(name: string, queryService: QueryService): boolean {
let val;
queryService.findValueByName(name)
.subscribe(
(res) => {
val= res!= null;
}, () => {
val= false;
}
);
return val;
}
//the function from the queryService looks like this
findValueByName(name: string): Observable<MyObject> {
return this.httpClient
.get<MyObject>('/rest/byName/' + name)
.map(result => {
if (<any>result) {
return new MyObject().deserialize(result);
}
})
}
The problem that I encounter is that the val from theValueExists is returned an undefined and I need for it to return the value true or false after the call to the backend is done and the result is in. Is there a way to do this?