Q:

Confusion with node.js asynchronous flow when using forEach.

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.

A:

The callback that you pass to Array.prototype.forEach() is synchronous. Therefore, it is blocking the execution thread until it finishes applying the function to all the members of the array.
I've also managed to find some interesting reads if you want to further investigate the asynchronous nature of JavaScript:

MDN - Array.prototype.forEach()
JavaScript, Node.js: is Array.forEach asynchronous? 
This thread addresses the same scenario that you are currently facing and 
it also provides an async alternative to Array.prototype.forEach()
Are all javascript callbacks asynchronous? If not, how do I know which are?

A:

certains functions are synchronous, others asynchronus. In fact, the Array.forEach method and all basic functions are synchronous. 
If you want manipulate array asynchronously, you have two options : call function in your forEach but you don't know when it's finished or, use async library.
First method : 
var a = [4, 5, 6, 7];
var results = [];

a.forEach(function(result){
  (function(i){
    results.push(i);
  }(result+1));
});

console.log(results);

With async.js : 
var async = require('async');
var a = [4, 5, 6, 7];
var results = [];

async.eachSeries(a, function(result, done) {
  results.push(result+1);
  done();
}, function () {
  console.log('forEach finished');
});
console.log(results);

