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
passesnull
err
on success.open
passesnull
data parameter on failure.
a couple of other points:
your code constructing
error
object, if things worked. wouldn't that, it's pointless.your code returning result of calling callback. that's unusual.
your code calling callback synchronously far can tell. (e.g., when calling
get
, callback occur beforeget
returns.) async call is, know, async. if you're doing things synchronously,opensync
, such, putsync
on name , return value directly rather calling callback.
Comments
Post a Comment