1

I have two individual arrays that I need to process. Since they are not codependent, I would like to do this asynchronously.

Here is my code:

cats = ['snowball', 'berlioz', 'garfield', 'lucifer'];
tigers = ['shere khan', 'hobbes', 'rajah'];

async.parallel({

        cats: async.apply(cats.forEach(function(item){

            if(item == 'garfield')
                console.log('hide your lasagna');
            else
                console.log('all safe');

        })),

        tigers: async.apply(tigers.map(function(item){

            if(item == 'hobbes')
                return 'eats tuna';
            else
                return 'eats people';

        }))

    }, function(error, results){

        if(error)
            console.log(error); return;

        meals = JSON.parse(results['tigers']);
        console.log('tiger meals: '+meals);

});

This is the error I get:

TypeError: Cannot call method 'apply' of undefined

What's going wrong?

Also, as a side question, how can I implement async.forEach and async.map here?

2 Answers 2

2

There are several major problems in your snippet.

  • async deals with asynchronous functions. Asynchronous functions cannot return values with the return keyword because that is a synchronous pattern. Asynchronous functions must take a callback function as their last argument and invoke it when done. Neither of your anonymous worker functions are in line with this.
  • Array.forEach doesn't return anything, but you are using it as if it did. async.apply expects a function as its first argument.

Start step by step:

Write a named function that does what you want in an asynchronous style on a single argument

function shouldWeHideTheLasagna(item, callback) {
   if (item === 'garfield') {
     process.nextTick(function () {
       callback(null, true);
     });
  } else {
    process.nextTick(function () {
      callback(null, false);
    });
  }
}

Learn how to use that directly without async. Then practice using that in combination with async.map, async.map(cats, shouldWeHideTheLasagna, function (error, results) {}); Then repeat for the tigers one and hopefully by now enough lightbulbs will be on that you will be ready to attempt a combination of async.parallel with sub-asyncs. But this is a tricky thing, so go slowly step by step and understand how each step works.

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

1 Comment

Those are some very nice pointers thank you! I can see now how I have mistaken "asynchronous" for "multi-threaded".
0

I think you are misinterpreting what async is used for. Async is meant to be used for functions that are already asynchronous, and provides organization for your callbacks. For example, if you needed to call three IO events in sequence, you could use Async.series to assure that each one is run after the previous without having to worry about callback hell

In your scenario, all of your functions are synchronous, and as such, async will not actually improve your runtime. I would instead recommend using the core Array prototypes such as Array.map and Array.forEach.

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.