Search code examples
javascriptreactive-programmingrxjsreactive-extensions-js

RxJS reduce doesn't continue


Why doesn't the flatMap cause downstream reductions to fire?

I got code like:

handleFiles.flatMap(files =>
  Rx.Observable.from(files).
  flatMap((file, i) => fileReader(file, i)).
  reduce((form, file, i) => {
    form.append('file[' + i + ']', result);
    console.log('reduce step', file);
    return form;
  }, new FormData()).
  tap(console.log.bind(console, 'after reduce'))
).
subscribe(console.log.bind(console, 'response'));

And the problem is that the 'after reduce' tap is never hit. Why?

The log is like:

reduce step [data]
reduce step [data]

Screenshot:

Error screenshot


Solution

  • If files is an array, then reduce should terminate if the observable returned from fileReader does. So for this code, the problem was that fileReader returned an observable that didn't complete.