Prevent nested subscriptions in your Angular code

When working with Angular you might find yourself in situations where you need something?from a first observable to be used in a second observable. Making you think the solution in “IMAGE A” seems to be the one to implement, Subscribing to the first observable and then using the data from it in the second observable generating a “Nested Subscription”.

No alt text provided for this image

Following good practices in Angular this should be avoided, due memory leaks making the inner subscription inaccessible to be unsubscribed.

In this cases you should use the “mergeMap” operator from RxJs as in “IMAGE B” generating this way a single subscription making it able to be unsubscribed whenever is needed.

No alt text provided for this image

要查看或添加评论,请登录

Patricio Mu?oz Fernandez的更多文章

社区洞察

其他会员也浏览了