最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - nodejs what is the difference between return callback() and just callback() - Stack Overflow

programmeradmin1浏览0评论

I think this might actually answer another of my questions on Stack Overflow if I can get this confirmed.

What is the difference between returning a callback and just calling a callback?

I have seen code doing either/or/both and trying to wrap my head around why and when to do which.

function test(x, y, callback){
    return callback(null, x);
    callback(null, x + y);
}

test(1, 2, function(err, results){
    if(!err){
        console.log('results:\n', results);
    } else {
        console.error('err:\n', err);
    }   
});

Edit: I think part of my questions resolves around my linked question, where I would constantly get an error, callback already called.

I was trying to understand the conceptual difference between these two different ways to do in my mind the same thing. Not sure why I am getting voted down when I ask a question to better understand a concept I haven't grasped yet. Aren't we all here to expand on our knowledge of programming by asking questions of better programmers than ourselves?

I think this might actually answer another of my questions on Stack Overflow if I can get this confirmed.

What is the difference between returning a callback and just calling a callback?

I have seen code doing either/or/both and trying to wrap my head around why and when to do which.

function test(x, y, callback){
    return callback(null, x);
    callback(null, x + y);
}

test(1, 2, function(err, results){
    if(!err){
        console.log('results:\n', results);
    } else {
        console.error('err:\n', err);
    }   
});

Edit: I think part of my questions resolves around my linked question, where I would constantly get an error, callback already called.

I was trying to understand the conceptual difference between these two different ways to do in my mind the same thing. Not sure why I am getting voted down when I ask a question to better understand a concept I haven't grasped yet. Aren't we all here to expand on our knowledge of programming by asking questions of better programmers than ourselves?

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Mar 28, 2017 at 16:38 shaunshaun 1,2732 gold badges19 silver badges46 bronze badges 2
  • 2 I think you may have over-simplified, since function test(x, y, callback) { return callback(null, x); } would be a fairly unusual thing to have. So the answer to the question actually asked probably won't help you much with your real problem. – T.J. Crowder Commented Mar 28, 2017 at 16:43
  • Does this answer your question? callback() or return callback() – krl Commented Dec 21, 2019 at 18:51
Add a comment  | 

4 Answers 4

Reset to default 11
return callback();
//some more lines of code;

the comment section won't be executed in above but will be in the below

callback();
//some more lines of code;

Edit :

Of course returning will help the context calling async function get the value returned by callback but usually async function are not called to assign anything.

Also, you use this trick usually with an if block, something like this :

asyncFunction (params, callback) {
if(err) callback(err, null);
callback(err, result);
}

To avoid callback being called twice you do something like this :

asyncFunction (params, callback) {
if(err) return callback(err, null);
callback(err, result);
}
  1. callback(); // execute
  2. return callback() // execute and pass whatever callback returns

So

function test(x, y, callback){
    return callback(null, x);
    callback(null, x + y); // never executed
}




// 1st example: 

test(1, 2, function(err, results) {
  if (!err) {
    console.log('results:\n', results);
  } else {
    console.error('err:\n', err);
  }
});

test(3, 4, function() {
  console.log("done")
}); // will alert done`

// 2nd example: 

console.log(test(5, 6, function() {
  return "done";
  // whatever here will not execute
})); // will alert done`

return callback(null, x);

does not return callback, but rather the result from calling the function. Whether or not this is a meaningful value depends on the implementation of callback.

If you simply call callback without returning the result, your function execution will continue as you do not return, and unless you return a value at a later point, test will return undefined in the end.

All in all, both approaches can make sense, however it heavily depends on your specific use case.

The only real question here is: do you want to have your callback dictate the return value of your outer method? If so, use return callback(...); otherwise, just use callback(...).

发布评论

评论列表(0)

  1. 暂无评论