Replies: 2 comments 3 replies
-
I don't think you can await an Observable because Observable can be never-ending. That being said if you have an observable-based HTTP client that you know is going to return 1 item only (like take(1) on all the calls) you could do this.
Promises can be awaited so just set up your observable and then add toPromise() at the end and you should now be able to await it. Another method would be doing
You could use tap to do something on "the side" with the response from the api.get. Since the tap method can not be awaited chaining 2 of these methods together will not guarantee order. Hopefully, that helps. |
Beta Was this translation helpful? Give feedback.
-
IMO rxjs is not the best choice if you want to use these kind of patterns. What's wrong with using a promise to load the list of students? The way I see how rxjs can be used in here is if you refactor the whole thing from the ground up. This pattern: loadStudents(){
return api.get('students').pipe(map(x => {
this.students = x;
}))
} is not something that RxJS was meant to do. RxJS is declarative, and lazy.... that's why I'd advise minimising side effects inside your operators. I feel it would be best represented as: this.students = api.get('students').pipe(
shareReplay() // In case you want to load just once and keep it cached forever.
) And you can keep adding in more features. e.g. Do you need to repeat the call to refresh the list every few seconds? There's an operator for that, The only problem this approach has is that If you want to get the value just once, then you have Edit - Please take my message as a kind advice. My point is that although you can use rxjs as the way of your original question, it will look odd in one way or the other. |
Beta Was this translation helpful? Give feedback.
-
consider the following async function using the promise pattern
how ever I cannot do this with cold observables(which majority of http functions are
i find myself having to do this alot
the other approach we have is to inline a dedicated subject as a flag
is there anything similar to below? or at least something i can use to compose a executeAsVoid operator?
Beta Was this translation helpful? Give feedback.
All reactions