Loop of subscribes inside a subscribe?

后端 未结 1 1948
粉色の甜心
粉色の甜心 2020-12-12 06:42

I\'m currently struggling with multiples/forEach subscribe inside a subscribe, I\'m trying to retreive a list of objects then retreive their images thanks to their ID. Curre

相关标签:
1条回答
  • 2020-12-12 07:07

    Since you already understood the usage of forkJoin, you are sort of on the right direction.

    RxJS's forkJoin() will for the Array.forEach() loop to be completed before returning all the observables. If you are familiar with the usage of Promises in JavaScript, it is actually similar to Promise.all.

    const observablesList = [];
    
    apps.forEach(app => {
      observablesList.push(this.appTypeService.getPhoto(app.Id));
    })
    
    forkJoin(observablesList).subscribe(response => {
      console.log(response);
      // handle the rest
    });
    
    0 讨论(0)
提交回复
热议问题