Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8624847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:38:01+00:00 2026-06-12T07:38:01+00:00

Having a divisive moment with my code. When doing an early return, which is

  • 0

Having a divisive moment with my code. When doing an early return, which is preferred?

if (err) callback(err);
else {
  ...
}

or

if (err) {
   callback(err)
   return
}
...

or

if (err) return callback(err);
...

However, I’m not comfortable returning callback since it’s a synchronous function. Which do you prefer? Why? Is there a technical reason as to which is preferred?

Ideally, I could do something like if (err) callback(err), return; but that’s not possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:38:02+00:00Added an answer on June 12, 2026 at 7:38 am

    My team and I settled on your last choice:

    function (x, y, z, cb) {
      var err, data;
      ...
      if (err) return cb(err);
    
      ...
      return cb(err, data);
    }
    

    The reasons are pretty straight forward. One, we’ve settled on “cb” for our callback – the consistency is very helpful. While cb isn’t returning anything it makes for an easy line to look at. EVERY TIME we use the the call back we want to make sure that we do a return because if we don’t it leads to really annoying errors and “return cb(err);” is short, easy to read, consistent, removes a level of indentation, and most importantly easy to look for.

    And, if you can get yourself into the habit of this or any other similar construct then it becomes second nature to look at your code and see where you have the stray “cb(err)” without the leading return.

    And, BTW,

    process.nextTick(function () { return cb(err); });
    

    is more efficient than

    setTimeout(function () { return cb(err); }, 0);
    

    And keep in mind, sometimes you need the nextTick call back to unwind your stack.

    And yes, we ALWAYS do a

    return cb(err);
    

    even when we don’t need to. Consistency is good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a code-blind moment. ASP.NET 4.0. Web.config: <?xml version=1.0?> <configuration> <system.web> <authentication mode=Forms> <forms
Having the following code, how can I have get receive a map, and return
Having trouble with each function... Will try to explain by example... In my code,
Having used storyboards for a while now I have found them extremely useful however,
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc
Having a duh moment trying to implement a new content page Here's the structure
Having the following code: template<typename T, typename OutStream = std::ostream> struct print { OutStream
Having the following code: #include <iostream> struct A { int x; A(){} ~A(){std::cout <<~A(<<x<<)\n;}
Having C++ console utility. The code inside parses the command line input and depending
Having this sample code: <input type=text id=changeid> <a href=# id=clickb>click</a> <script> $('#clickb').on(click, function(event){ alert(1);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.