Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run several asynchronous functions in parallel

I have one object which contains two different methods

var samp {
 m1: func1,
 m2: func2
}

Depending on the number of keys I want to call all the three function parallely Now Im using the following code which runs serially.

switch (sampType) {
            case "m1":
            {
                return new func1();
                break;
            }
            case "m2":
            {
                return new func2();
                break;
            }
            default:
            {
            }
        }

How can I execute all the methods parallely in node.js? Any help on this will be really helpful

like image 890
user87267867 Avatar asked Apr 24 '26 17:04

user87267867


1 Answers

Check out async.parallel. You would essentially write:

async.parallel( [

    function ( callback ) {
        // Code
    },

    function ( callback ) {
        // Code
    }

], function ( error, results ) {
    // Both done
} );
like image 171
fakewaffle Avatar answered Apr 26 '26 08:04

fakewaffle