javascript - Should a callback be passed null or undefined when there is no error? -


i'm hoping simple question. accepted best practice callbacks?

option 1:

function (id, callback) {     var topic = find(id);     var err = new error('sorry, ' + id + ' not valid id.');     err.status = 404;     return (topic) ? callback(null, topic) : callback(err); } 

option 2:

function (id, callback) {     var topic = find(id);     var err = new error('sorry, ' + id + ' not valid id.');     err.status = 404;     return (topic) ? callback(undefined, topic) : callback(err); } 

side note, find() returns undefined, not null.

thanks in advance.

i node built-in api functions do.

a trivial experiment tells me that:

  • open passes null err on success.

  • open passes null data parameter on failure.


a couple of other points:

  1. your code constructing error object, if things worked. wouldn't that, it's pointless.

  2. your code returning result of calling callback. that's unusual.

  3. your code calling callback synchronously far can tell. (e.g., when calling get, callback occur before get returns.) async call is, know, async. if you're doing things synchronously, opensync , such, put sync on name , return value directly rather calling callback.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -