admin管理员组

文章数量:1431004

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.

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.

Share Improve this question edited Jan 10, 2016 at 16:24 rex asked Jan 10, 2016 at 14:59 rexrex 3,1837 gold badges38 silver badges63 bronze badges 5
  • That depends, do you want to return something other than undefined to getData ? – adeneo Commented Jan 10, 2016 at 15:01
  • oh so that's what it depends on? – rex 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. – Mike Cluck 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 – adeneo 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. – Mike Cluck Commented Jan 10, 2016 at 15:06
Add a ment  | 

3 Answers 3

Reset to default 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')
}

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) putation 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.

You don't have to, but you might run into trouble if you don't use 'return'. For example, error handling can bee 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 acplish the same thing with if/else blocks and no return statements.

本文标签: angularjsShould I use 3939returnquot with my callback function in JavascriptStack Overflow