So I'm using forkJoin to complete a dynamic amount of requests. It's working great, but I'm not getting my console log when subscribing to the forkJoin? All the examples I've seen just do a basic subscribe like I am, so I'm not really understanding why it isn't working.
I've added the DataService calls also for more clarity. Perhaps I'm doing something wrong there and that's why the forkJoin isn't working?
Here is my code:
let requests = [];
newAdds.forEach(ele => {
requests.push(this._dataService.add(ID1, ID2));
});
forkJoin(requests).subscribe(ret => {
console.log(ret);
}
);
add(id1, id2) {
class Add{
ID1: number;
ID2: string;
};
let data = new Add();
data.ID1 = id1;
data.ID2 = id2;
let cmd = "Add";
return this.put(this.urlAdmin() + cmd, data);
}
protected put(cmd: string, body: any) {
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this._http.put(cmd, body, {
headers: headers
});
}
forkJoin will only emit if and only if all observables have completed.
This one example will never emit (interval never ends)
forkJoin([interval(1000), interval(2000)]).subscribe(console.log); // won't emit
This example will emit [0, 0] value after 2000ms.
forkJoin([timer(1000), timer(2000)]).subscribe(console.log); // emit [0,0]
If one observable does't emit a single value but completes, forkJoin won't emit
forkJoin(of()).subscribe(console.log) // won't emit
If at least 1 observable throws an error, forkJoin will never complete.
forkJoin([of(1), throwError(1)]).subscribe(console.log); // will never emit