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
togetData
? – 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
3 Answers
Reset to default 3If 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
版权声明:本文标题:angularjs - Should I use ''return" with my callback function in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745559978a2663416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论