We often need to make sure that execution won't return to a function after a callback has been made. My question is, are there any subtle differences in effect between these two styles, or are they entirely the same? :
function myFunction(foo, callback) {
if(foo) {
callback(err, true); //Here
return; //Here
}
bar();
}
And:
function myFunction(foo, callback) {
if(foo) {
return callback(err, true); //Here
}
bar();
}
EDIT: Sorry, I realised that my question wasn't very clear. I'm talking about situations where I don't care what is being returned (because it won't be consumed synchronously), but where the return is simply to ensure that execution does not continue in the function, after the callback function has completed.
It entirely depends on what you want to return.
If you want to return the value returned by the callback use second method else first
As a matter of coding style, if one wants to avoid returns within a function, which some people consider a Bad Thing, then
function myFunction(foo, callback) {
if(foo) {
callback(err, true);
} else {
bar();
}
}