Reputation:
I have following issue am I doing something wrong because "done" isn't invoked:
@Injectable({
providedIn: 'root'
})
export class GetPersonsService {
url:string="https://swapi.co/api/people/"
persons:Person[];
headers: HttpHeaders = new HttpHeaders()
.set('Accept', 'application/json');
constructor(private http: HttpClient) { }
getPerson(personsId){
return this.http.get<Person[]>(`${this.url}${personsId}/`,{headers:this.headers});
}
getAllPersons(){
let numberOfPersons=88;
const response = [...Array(numberOfPersons).keys()].map(i => this.getPerson(i+1));
return forkJoin(response).pipe(map(value=> value),share());
}
}
and MainComponent
export class MainComponent implements OnInit {
persons=[];
constructor(private getPersons:GetPersonsService) { }
ngOnInit() {
this.getPersons.getAllPersons().subscribe(value=>{
// this.persons.push(value);
console.log("done");
}
);
}
}
What is going on here?Why I am not getting done in console
Upvotes: 0
Views: 71
Reputation: 2761
You don't create a proper array... Use Array constructor with amount of index, fill it with dummy data (null) and loop over it with map. RxJS map operator is useless.
getAllPersons(){
const numberOfPersons = 10;
const response = Array(numberOfPersons).fill(null).map((_, i) => this.getPerson(i+1));
return forkJoin(response).pipe(share());
}
https://stackblitz.com/edit/angular-ocbfoy
UPDATE
It seems user 17 does not exist. In this case, please find below the update code :
getAllPersons(){
const numberOfPersons = 88;
const response = Array(numberOfPersons)
.fill(null).map((_, i) => this.getPerson(i+1)
.pipe(catchError(() => of(null))
)
);
return forkJoin(response).pipe(share());
}
Upvotes: 1