0

I have the below code which check for each user in an array. If found, the switch case will work accordingly.

var users = ["A","B"];
function searchUser() {
for (var j = 0; j < users.length; j++) {
        execute(users[j]);
    }
}

function execute() {
switch (selectUser) {
            case "A":
            {
                return new A();
                break;
            }
            case "B":
            {
                return new B();
                break;
            }
            ............
            }
}

How can I make it run parallel. Now Im getting the output of A() and then B(). But I want it to happen vice versa also.

2 Answers 2

1

Though you cannot make them running truly in parallel, you can make it more asynchronous with the use of async module: https://github.com/caolan/async .

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

3 Comments

If you want to call execute for each element of users array, try something like async.parallel(users.map(function(user){return function(callback){execute(user)};})); You will also need to install async with npm, and call require('async') - see the documentation in the link I send for details.
var async = require('async'); var users = ["A","B"]; function execute(selectUser) { switch (selectUser) { case "A": { console.log("Helloo"); break; } case "B": { console.log("Hiii"); break; } } } async.parallel(users.map(function(user){return function(callback){execute(user)};}))
Yet im getting result "Helloo","Hiii"
0

This code looks CPU intensive and probably does not have IO involved. The node is single-threaded model so parallel processing cannot on a single core (for multiple cores you need to start multiple node processes). On the single core the throughput increases only if you have IO involved. If your present code does not have IO involved, you may not gain anything.

Comments

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.