Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I hate functions and re-writing callbacks by "intercepting" them and passing the original along. This is simple: it uses an existing function, exec, and doesn't open it up. Its callback arguments are passed into results.exec. There's just a lot of overhead here. I was wondering if there's an easier way, using the async library or not.

function execModify(command, manipulators, callback) {
  var exec = childProcess.exec;
  return async.auto({
    "exec": async.apply(exec, command),
  }, function(err, results) {
    if (err) return callback(err);
    var value = results.exec[0];
    if (typeof manipulators == "function") return manipulators(null, value);
    if (manipulators.length == 0) return callback(null, value);
    _.each(manipulators, function(manipulator) {
      value = manipulator(value);
    });
    return callback(null, value);
  });
};
share|improve this question
    
It sounds like you want promises. – jfriend00 Dec 1 '14 at 21:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.