Nov 15th, 2016

Assuming there was no fatal error, should the first argument to an asynchronous callback be `undefined` or `null`?

Best practices for passing the first argument to an asynchronous callback

This is the short and sweet version. If you find it helpful, you might want to check out the entire gist.

In your implementation, if you always do one of the following, you’ll be good to go, and you’ll provide a better experience for the people using your thing:

1. Invoke callback with only

Some kind of fatal error occurred– something where I’d normally throw. Except that I’m an asynchronous function. So instead, I do this:

return cb(new Error('Something bad happened!'));

This is like doing in a synchronous function.

2. Invoke callback with no arguments

Everything is AOK, and there’s no other data to send back:

return cb();

This is like doing in a synchronous function.

3. Invoke callback with and

Everything is AOK, and there’s this result I want to send back:

return cb(undefined, yourResult);

This is like doing in a synchronous function.

We make Sails work and things work with Sails.

We make Sails work and things work with Sails.