How to fix jslint warning Don't make functions within a loop

Getting this warning on the following code:

  workflow.removeZSets = function(fn) {
    var processed = 0;
    for (var c = 1; c < 10; c++) {
        workflow.removeZSet(c, function() {
            processed++;
            if (processed === 9) {
                return fn(null, "finished removing");
            }
        });
    }
}

workflow.removeZSet = function(precision, fn) {
    rc.zrem("userloc:" + precision, function() {

                return fn(null, 'done');
        });
    });
}

Does anyone have a suggestion how to accomplish this without triggering the warning?

I have some ideas like using the async library to run them all in parallel but this is a fairly common thing I do throughout this code base so interested in feedback on the best way.

The error is because you have define a function within your for loop.

You could try something like this, defining the function outside of the loop:

workflow.removeZSets = function(fn) {
  var processed = 0;

  function removeZ(c) {
    workflow.removeZSet(c, function(err) {
      processed++;
      if (processed === 9) {
        return fn(null, "finished removing");
      }
    });
  }

  for (var c = 1; c < 10; c++) {
    removeZ(c);
  }
}

Using a library like async to do the looping would help clean up your code, it would allow you to avoid checking if all the items have processed (processed ===9) because it is handled by async.