Kinvey doesn't currently support backend cron like functionality. So, I'm rolling my own. Normally, I'd use a LAMP stack to do this. However, since I'm going all in with AngularjS, I thought I'd use nodejs for this as well.

Let's just say my JavaScript skills are a bit rough. So when trying to accomplish what would have been a very easy task in PHP; I ran into a lot of hurdles. I'm looking at you, asynchronous processing!

Then, I remembered that Kinvey uses the excellent async utility to ease the pain. async.each was just what I needed.

Or so I thought ... I won't tell you how many hours I wasted trying to get this to work for my needs. Again, this mainly comes down to my mediocre JavaScript skills and my habit of reading documentation very cursorily.

However, I also feel part of the problem was with the async documentation. The developer & contributors are clearly excellent programmers. They develop at a level over the heads of many like myself. Because of that, they might fail to realize the rest of us need some more documentation.

This little example snippet might work for JavaScript ninjas, but it failed me miserably.

async.each(openFiles, saveFile, function(err){
    // if any of the saves produced an error, err would equal that error
});

After FINALLY overcoming my hurdles (I wasn't using the callback), I decided to try to help out. I've forked the async repository, added a more in-depth example, and submitted a Pull Request (#401).

In the meantime, if you are looking for an idea of how to iterate an array using the async utility, here is my sample:

async.each(openFiles, function( file, callback) {
  
  // Perform operation on file here.
  console.log('Processing file ' + file);
  callback();

  if( file.length > 32 ) {
    console.log('This file name is too long');
    callback('File name too long');

    return;
  } else {
    console.log('File saved');
    callback();
  }
}, function(err){
    // if any of the saves produced an error, err would equal that error
    if( err ) {
      // One of the iterations produced an error.
      // All processing will now stop.
      console.log('A file failed to process');
    } else {
      console.log('All files have been processed successfully');
    }
});

Hopefully this will keep you from spending hours of banging your head against the wall like I did.

'#savethewalls'