I have come across this particular reduce function being used as an example of mapreduce in MongoDB more than once:
function reduce(key, values) {
var result = {count:0};
values.forEach(function(value) {
result.count += value.count;
});
return result;
}
However, I find it rather strange. The iteration is performed using the .forEach() method with a callback function for counting. Yet, we immediately return result;.
Is it possible that there are situations where we return the result variable before the callback has finished looping through the values?
My understanding was that callbacks are meant to be delegated to another (potentially) separate thread while the main control flow continues normally.