If your function only has one path, you can use both forms interchangeably. The return value from the function will be undefined without the return statement, but your calling code probably doesn't require it anyways.
It's important to note that:
return callback()
is essentially the same as
callback(result); return;
The latter does create an extra frame on the call stack, using more resources. If you have deeply nested callbacks or recursion, you may exhaust the stack space more quickly.
In general, inserting a return before the callback is considered more idiomatic and easier to understand.
When your function has multiple paths, caution is required. For example:
(cb)=> {
if (something) cb('a')
else cb('b')
}
This will work as expected. However, in this scenario, both callbacks will be executed:
(cb)=> {
if (something) cb('a');
cb('b')
}
It's evident from the code above that both callbacks will be called. Writing code like this is a common mistake for newcomers to Node.js, especially when handling errors. To ensure only one callback is executed, you should modify the code as follows:
(cb)=> {
if (something) return cb('a');
cb('b')
}