async.parallel not working like asynchronous in node js

I have a below code.

var async = require('async');

async.parallel({

        f2: function(callback){
            for (var i=0;i< 100000000;i++){

            }
            console.log("f2");
            callback(null,"function 2");
        },
        f1: function(callback){

            console.log("f1");
            callback(null,"function 1");
        },

    },
    function(err, results) {
        console.log(results);
    });

and I run above...

result:

f2
f1
{ f2: 'function 2', f1: 'function 1' }

Why first result f2 ? Why not f1? f1 function is simple more than f2 function.

I think.. I cant make like asynchronous.

I don't want use SetTimeOut , proccess.NextTick etc...

Upvotes: 0

Views: 441

Answers (1)

AlexBenoit
AlexBenoit

Reputation: 66

The function parallel() is about kicking-off I/O tasks in parallel, not about parallel execution of code. If your tasks do not use any timers or perform any I/O, they will actually be executed in series. Any synchronous setup sections for each task will happen one after the other. JavaScript remains single-threaded. Reference: https://caolan.github.io/async/docs.html#parallel

Upvotes: 1

Related Questions