0

enter image description here

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.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Richard
  • 556
  • 5
  • 12
  • Please read this. https://stackoverflow.com/questions/40861494/angular2-unsubscribe-from-http-observable-in-service – xdeepakv Feb 15 '20 at 07:04
  • You don't have to unsubscribe. take(1) makes it clear that observable will complete after one emission – cuddlemeister Feb 15 '20 at 08:58
  • @xdeepakv I am not doing this in a service.ts – Richard Feb 15 '20 at 20:28
  • @cuddlemeister Thanks, in case of Question1, do I need to unsubscribe if I don't have take(1)? even if it is wrapped in a Promise? – Richard Feb 15 '20 at 20:29
  • @Richard well, http module provides observables, that fire only once. But it's a good practice to always make sure you won't have any memory leaks due to not-unsubscribed observables. Wrapping observable in a promise is not a good idea, since it's much better to have it like you did in `Question2`. Answering your question: when you wrap observable in a promise, you will still have an observable, so ye, you need to unsubscribe or use `take(1)` – cuddlemeister Feb 16 '20 at 13:55

1 Answers1

0

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).

martin
  • 93,354
  • 25
  • 191
  • 226
  • getPassengersByBid() is an infinite Observable. In your last paragraph, it seems like toPromise() won't work on infinite Observables because they never complete. So I think I do need to wrap the Observable in a Promise, plus I need to edit the return data from the Observable and return something different as a Promise. Anyway, your answer clears a lot of things up for me. Thank you. – Richard Feb 15 '20 at 20:36
  • Answer to my own comment. toPromise() does work on infinite Observable only if you use it with take(1) I believe. But I don't want to return the data from the Observable as the return data of the Promise. That is why I need to wrap it in a "new Promise()" so that I can edit the data and return whatever I want as the Promise. Someone please let me know if my logic is correct. Thanks – Richard Feb 15 '20 at 20:43
  • You're right. If your Observable is infinite then you have to use `take(1)` because otherwise `toPromise()` will never resolve. You can use other operators after `take(1)` to transform data to whatever you want and still use `toPromise()`. – martin Feb 16 '20 at 08:05