1

I have a codebase which contains code similar to the code below many times:

function(doc, callback) {
  doSomething(function(err) {
    if(err) return callback(err);

    callback(null, doc);
  });
}

I'm wondering if there are any downsides to just combining the explicit error check into:

function(doc, callback) {
  doSomething(function(err) {        
    callback(err, doc);
  });
}

I understand that callback handlers are expected to check the err on callback, but in this case it's just bubbling up.

I suppose I'm wondering if based on the way callbacks are generally used, if this is an issue?

1
  • your approach is fine if you don't need to handle (err, doc) before calling back Commented Jan 8, 2014 at 17:27

1 Answer 1

1

There is no difference, the code is doing the same thing. First one is just easier to edit later if you want to add some postprocessing.

Technically, second example provides a "doc" and first don't, but if somebody rely on that, they're doing it very wrong.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.