I am coding in node.js for 6 months. I have read about concept of asynchronous coding, event loops and callbacks and so on. I know " asynchronous code will never execute while the synchronous code stack is executing. This is the meaning of node.js being single-threaded". But here is one example
var a = [4, 5, 6, 7];
var results = [];
a.forEach(function(result){
results.push(result + 1);
});
console.log(results);
As far as I understand, argument to a function forEach is callback which is called later after the synchronous block finishes executing. That means I expect the result will be
[]
but it is infact
[5, 6 ,7, 8]
Why is this happening? Is forEach synchronous? Or I am missing something? In my understanding,
console.log(results)
Will be executed before data is pushed into it.