0

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...

2
  • async is for managing asynchronous code. Your code isn't asynchronous. Commented Aug 24, 2017 at 18:16
  • please indent your code properly Commented Aug 25, 2017 at 7:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I dont think so.. they dont start same time.
I can not see the console log of the f1 function before the f2 function is finished

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.