I'm recursively calling a function and its callback, the done() function, is being called infinitely and I don't know why.
When I log i, it reaches the length of data and then the condition is met and it stops, but the done function is called infinitely. The done function is not recursive; why is it being called more than once? How can I get it to be called only once when the incrementer is equal to the length of data and nTwo is defined?
I think it may be because of the pre-increment of i, but I needed that otherwise I get an RangeError, Maximum Stack Exceeded.
function train(i, data, n, nTwo, func){
console.log(i, i===data.length);
if(i===data.length && nTwo===undefined) func();
else if(i<data.length) (new Trainer(n)).workerTrain([data[i]], train(++i, trainingSet, l, y));
else done();
}
function done(){
console.log('first set of workers done');
saveAs(new Blob([JSON.stringify(l.toJSON())], {type: "application/json"}), "l.json");
train(0, yonTraining, y, undefined, finalTrainingCallback);
}