Reputation: 1209
More than once have I seen the following reduce function as a mapreduce usage example for mongodb:
function reduce(key, values) {
var result = {count:0};
values.forEach(function(value) {
result.count += value.count;
});
return result;
}
But this strikes me as very odd. The iterating is done with the .forEach() method which uses a callback function to do the counting. However, we return result; right away.
Can't it be the case sometimes that we return the result variable before the callback is done iterating through the values?
I thought the purpose of callbacks is that we delegate it to a (possibly) different thread while the main control flow continues normally.
Upvotes: 0
Views: 382
Reputation: 72967
forEach
isn't asynchronous.
The return
statement will only be executed after the forEach
is done.
Upvotes: 3