Q:

Working on arrays with async.parallel

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?

A:

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.

