0

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?

1 Answer 1

1

findValueByName is an asynchronous operation. Withint theValueExits you do return val; before the subsciption block is executed.

queryService.findValueByName(name)
            .subscribe(
            (res) => {
                val= res!= null;
            }, () => {
                val= false;
            }
        );

The best approach would be to define further observables based on the others. theValueExists will become

private theValueExists(name: string, queryService: QueryService): Observable<boolean> {
  return queryService.findValueByName(name).pipe(
    map(res => res != null),
    catchError(err => of(false)),
  );

And utilAccountName would also need to handle thoseobservables with respective rxjs utils.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain to me what are you doing with pipe? I never seen anything like this and it seems odd that you have , and after that nothing. And it still requires Observable<MyObject>. Is some part of your answer missing?
pipeis a method of Observable and is required to chain rxjs operators to transform the stream of data. Please get in touch with rxjs basics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.