Do I need to unsubscribe any of the above Observables? What does take(1) really do? Lastly, how do you use async-await in this context? (Is it better to use async-await?)
New to Angular. Please help. Thank you.
Do I need to unsubscribe any of the above Observables? What does take(1) really do? Lastly, how do you use async-await in this context? (Is it better to use async-await?)
New to Angular. Please help. Thank you.
If you need take(1)
really depends on what getPassengersByBid
does. If this method returns an Observable that emits just one item and then completes you don't need to use take(1)
because it completes itself. For example httpClient.get()
makes one next
and one complete
so you don't need to chain it with take(1)
.
If getPassengersByBid
is an infinite Observable that never completes then it makes sense to use take(1)
if you want just one value.
Second thing is that you don't need to wrap Observables with Promises and you can use just toPromise()
. However, be aware that with toPromise()
the source Observable has to complete and that the Promise will be resolved with the most recent value (but if you use take(1)
you'll have the same behavior to what you have right now even with Observables that emit multiple items).