Angular5 RXJS 递归 http 请求

Angular5 RXJS recursive http requests

我目前的情况是:

#Service My Service
private users = ['user1','user2'];

//Generate list of requests to join
private getHttpList(): any[] {
  let gets = new Array();
  for(let index in this.users)
      gets.push(this.http.get('https://api.github.com/users/' + this.users[index]))
  return gets;
}
...
getList(): Observable<any[]> {
    return forkJoin(this.getHttpList())
}

在我的组件中,我执行订阅

this.MyService.getList().subscribe(results => {
    for(let res in results) {
       //...Do something here
       //..I wanna do the get in of https://api.github.com/users/{user}/starred
    }
})

假设我只知道getList()的结果后的"starred url",我该如何"synchronous"这部分,或者说正确的形式是什么?

**我尝试硬编码——结果 id 错误,因为 "res" 是 "interable"

this.MyService.getList().subscribe(results => {
        let url = 'https://api.github.com/users/';
        for(let res in results) {//This don't do the things "synchronous"
           this.http.get(url + res.login +'/starred').catch(err => {
               throw new Error(err.message);
           }).subscribe(starred_res => {
               //So we set the starred_list
               res.starred_list = starred_res 
           })
        }
    })

谢谢...

据我了解,您希望获得每个用户的星标列表。

最简单的方法是获取所有加星标的列表,然后将它们与用户结果匹配。

// Get users
this.MyService.getList().subscribe((results: any[]) => {

  const url = 'https://api.github.com/users/';

  // Create requests to get starred list for every user
  const starredRequests = results.map(
    res => this.http.get('https://api.github.com/users/' + res.login + '/starred')
  );

  // Wait when all starred requests done and map them with results array
  Observable.forkJoin(starredRequests).subscribe(starred => {

    results.forEach((res, index) => {
      res.starred_list = starred[index];
    });

    console.log(results);
  });

});