0

I have an array of homogeneous objects. I want to be able to run a a method of each of the objects in parallel. If I were to do this synchronously, it would look like:

var objs = [{run:function(){}},{run:function(){}},{run:function(){}}];

for (obj in objs) {
    obj.run();
}

I have thought about using async library, but don't see anything in there at jumps out at me that would work.

1
  • 4
    If the functions aren't async, they'd still run synchronously as there's only one thread in Node JS apps. Commented Sep 26, 2013 at 18:32

1 Answer 1

1

You can map your objects to functions first, and then call these functions in parallel using async.parallel

var async = require('async')

var objs = [{run:function(){}},{run:function(){}},{run:function(){}}];
var func2call = objs.map(function(x){return x.run})
async.parallel(func2call);
Sign up to request clarification or add additional context in comments.

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.