Rx.Observable.forkJoin with empty list as parameter not calling complete
Author: airiciucCreated Aug 18, 2017Updated Mar 2, 2018
Assume the following scenario where you need a dynamic number of server calls (sometime is 0) and you need to execute some functionality when all of them are done.
let updatesObservable = getRequiredUpdates();
Rx.Observable.forkJoin(...updatesObservable)
.subscribe(() => onUpdatesFinished());
function getRequiredUpdates() {
//dinamically calculate the required updates and return a list of Observables for each ajax call
return [].map(v => Rx.Observable.return(v).map(u => processUpdate(u)));
}
function processUpdate(u) {
//process each update
console.log(u);
}
function onUpdatesFinished() {
//do here some important stuff when all updates finished
console.log("All updates done");
}Expected behavior:
processUpdatefunction is never calledonUpdatesFinishedis called once
Actual behavior:
processUpdatefunction is never calledonUpdatesFinishedis nerver called
Note: Change return [].map(v => Rx.Observable.return(v).map(u => processUpdate(u))); to
return [1, 2, 3].map(v => Rx.Observable.return(v).map(u => processUpdate(u))); and onUpdatesFinished is called once
Source: Reactive-Extensions/RxJS