Angular & RxJS Tips #5: Multiple Http calls - combineLatest vs forkJoin
ฝัง
- เผยแพร่เมื่อ 25 ส.ค. 2024
- In RxJS you can make multiple REST calls, wait that all of them are completed and get a single result.
You can use forkJoin and combineLatest RxJS operators to achieve this result.
So, what’s the difference?
Level: intermediate
----
Follow me on:
• LinkedIn: / fabiobiondi
• Twitch: / fabio_biondi
these short videos are programming gems, please, keep doing them! thanks for your time!
this is a very precise and concise video, without using those default examples, excellent !!
I love this video! Thank you so much
broooooooooooooo, This Explain are amaziiiiiiing.
thank you for the feedback Ahmad
Hey, it is little bit difficult to understand the accent but quality of content is very good. By the way what is your native language?
thank you. I'm trying to improve my english but it will be a looong journey 🤣
complex things
Can you help me on a different situation please, that is not really nested calls but instead a sequence of call where the results have to be concatenated. I call service.getItems(p1).suscribe(res=>{this.allItems = res; service.getItems(p2).subscribe(res =>{this.allItems = this.allItems.concat(res); ... many nested calls, you get?
If you need to concat previous result with the new one you should do something similar (hard to write here in a YT comment).
If you prepare a stackblitz with a real world case I can help you more.
service.getItems(p1)
.pipe(
mergeMap(res => service.getItems2(res).pipe(map(res2 => [...res2, ...res2]) )
.. so the next operator will receive the merge of previous results and you can do another mergeMap doing the same...
)
.subscribe(res => {
// res is now the merge of previous results
})
@@FabioBiondi so many thanks for the quick answer, will have a try and prepare a stackblitz for sure, as I am procratinating this issue for too long
@@FabioBiondi I made you editor of a stackblitz Data API Rest Angular (forked)
@@MikeDePetris where can I find it?
@@FabioBiondi When i use mergeMap, i always make sure the inner observable is a completed observable. If not, will have a memory leak ! Is it a right things ?