Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rxjs, forkJoin(array).mergeMap(...), inner mergeMap never get called

Tags:

rxjs

rxjs5

In my case there are multiple requests could be performed in parallel at first, after those requests complete, another request will be sent with previous result, the pseudo code would look like

let uploads$ = [obs1$, obs2$, obs3$];
Observable.forkJoin(uploads$).mergeMap(
  res => {
    // never get called if uploads$ = []
    let data = someCalculation(res);
    return this.http.post('http://endpoint/api/resource', data);
  }
).subscribe(
  res => {
  }
);

If uploads$ = [], the inner mergeMap never got called.

Can someone help? I'm on RxJS 5.4

like image 499
fifth Avatar asked Jan 03 '23 10:01

fifth


1 Answers

It's not called b/c there is no emission on the source observable. To create one on, if observables is empty you can use the defaultIfEmpty or toArray operators.

const observables = [];
Rx.Observable.forkJoin(observables)
  .defaultIfEmpty([]) // or .toArray()
  .mergeMap(results => Rx.Observable.of(results.length))
  .subscribe(console.log);
like image 176
mkulke Avatar answered Feb 24 '23 04:02

mkulke