Issue
Assuming I have this code
observableMethod(): Observably { ... Return of([1, 2, 3]); }
notObservableMethod(): integer {
Let myVal;
if (isOM) { this.observableMethod().pipe( first() ).subscribe( val => myVal = val; }); } else { myVal = this.nonAsyncMethod(); }
return myVal * 5; }
What can I do to notObservableMethod so it can return myVal * 5;? Meaning it has to be a syncronous method?
Edit: In reality, val is a url and I have a postProcess for that url that is more complex in the example I posted above before I navigate to it. Currently, what I did is create another method for the post process then call it in each condition or in a subscribe if its observable but I was thinking if there's a better way of doing it. Thank you!
Solution
Since I am not entirely sure I understand the whole snippet, I'll try to guide you based on your example. I was trying to figure out if there could be a better way by using some RxJS operators along the way, before you subscribe, or a async$
pipe in your template.
I would change your code above to something like this:
observableMethod(): Observably { ... Return of([1, 2, 3]); }
notObservableMethod(): Observable<number> {
const observableSource = isOM ? this.observableMethod() : Observable.of(nonAsyncMethod());
return observableSource.pipe(first(), map(val => val * 5));
}
Answered By - Eren Yeager
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.