3

I am using asynchronous functions in my JS application wrapped with my own functions that receive callback inputs. When i call the callback function, do I need to use the "return" keyword? does it matter? whats the difference?

eg:

var getData = function(callback){
    // do some aysnc db stuff...
    return callback(results);
    // or just
    callback(results);
}

PS: I am writing a hybrid mobile appiliction using javascript.

5
  • That depends, do you want to return something other than undefined to getData ? Commented Jan 10, 2016 at 15:01
  • oh so that's what it depends on? Commented Jan 10, 2016 at 15:02
  • @adeneo No, that's the async function. He's not returning to that value. It doesn't matter which one you use. You can use return to exit the function early otherwise it doesn't matter. Commented Jan 10, 2016 at 15:02
  • @MikeC - TL;DR, it just looked so synchronous, maybe the OP should throw a timeout in there to make it clearer Commented Jan 10, 2016 at 15:04
  • @adeneo True and I guess theoretically they could return something from it in addition to running their callback (assuming the callback was inside of something actually asynchronous such as setTimeout) so it works both ways. Commented Jan 10, 2016 at 15:06

3 Answers 3

3

If you only have one path through your function then you can use both forms pretty much interchangeably. Of course, the return value from the function will be undefined without the return, but your calling code is probably not using it anyway.

It's true that

return callback()

is practically equivalent to

callback(result); return;

The latter does result in an additional frame on the call stack, and so uses more resources. I suppose if you had many nested callbacks, or were doing recursion, you'd run out of stack space more quickly.

It's probably a bad idea and I don't think I'm sticking my neck out in saying that the return before the callback is way more idiomatic.

When you have multiple paths in your function, you have to be careful. For example, this will work as you expect:

(cb)=> {
    if (something) cb('a')
    else cb('b')
}

However, in this case, both callbacks will be called.

(cb)=> {
    if (something) cb('a');

    cb('b')
}

When you read the above, it's pretty clear that both will be called. Yet writing code like that is a classic node newbie mistake (especially when handling errors). If you want either or to be executed you need:

(cb)=> {
    if (something) return cb('a');

    cb('b')
}
Sign up to request clarification or add additional context in comments.

Comments

3

No, callback should not be used with return.
I'd never expect a callback-function to return a value. The callback replaces the return in terms of passing a value when the (async) computation is done.

You can use this Syntax return callback(result) as a shortcut for like callback(result); return; but it may confuse some further Team-member, what kind of value callback might return.
This is actually a task for your minifyer, to create such code; not yours.

Comments

0

You don't have to, but you might run into trouble if you don't use 'return'. For example, error handling can become problematic if you're not used to making early returns. As an example:

var getData = function(callback){
  dbStuff(function(err, results) {
    if (err) { callback(err, null); }
    callback(null, results);
  });
}

I generally consider it good practice to return when you are done...

var getData = function(callback){
  dbStuff(function(err, results) {
    if (err) { return callback(err, null); }
    return callback(null, results);
  });
}

But you can accomplish the same thing with if/else blocks and no return statements.

5 Comments

this is bull. i'd never expect a callback-function to return a value. the callback replaces the return in terms of passing a value when computation is done. In your example is the second return obsolete and the first one just a different way to turn the rest of the function into an else-block. it's more likely to understand what there is happening when you write if(err){ callback(err, null); return; } instead of implying that the callback-method might return a result.
It becomes a question of preference. I like to be able to debug one function at a time by ensuring that it returned a call to the one I expected.
what do you mean with by ensuring that it returned a call? You can call a function and you can return a value.
And yeah, it is a matter of preference, but then everyone should know, that this is just a lazy shortcut and sth. i'd consider a bad practice like assigning a value inside of a condition. It might lead to confusion.
That's fair. I'm used to writing it this way because it makes sense semantically to me. One isn't able to "purely" test a function in isolation without such a return value, as you'll otherwise have to spy on a second function to ensure it was called from the primary. If all your function does is return a call to another function, why not write it to say that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.