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 6871975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:52:09+00:00 2026-05-27T03:52:09+00:00

Which function callback structure is the best to use in Javascript and why? I

  • 0

Which function callback structure is the best to use in Javascript and why? I have seen these two options use quite a lot. Are there any more?

Option A:

// DECLARATION
function funcA(required, success, error, options){

  // if there is an error in your function return 
  // and run error function
  if(errorHappens){ return error('an error') };

  // if no error happened: run the success function
  success('all good here is your var back', required);
}

.

// USAGE
funcA('this is required', function(result){

  // all good, do something with the result :)

},
function(error){

  // tell the user about the error
  alert('error happened')  
},{
  optionalThing: ':-)'
});

Option B:

// DECLARATION
function funcB(required, callback, options){

  // if an error happens run the single callback with
  // the err param populated and return
  if(errorHappens){ return callback('an error') };

  // if no error happened: run the callback with 'null'
  // as the error param.
  callback(null, 'this is a result');
}

.

// USAGE
funcB('this is required', function(err, result){
  if(err){ return alert('error happened') };

  // all good do something with the result :)

},{
  optionalThing: ':-)'
}
  • 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-05-27T03:52:10+00:00Added an answer on May 27, 2026 at 3:52 am

    It depends on the environment, for node.js

    I would personally recommend you stick to callback as the last parameter

    function doSomething(data, options, cb) {
      ...
      if (err) return cb(err);
      cb(null, result);
    }
    
    doSomething(data, { ... }, function (err, data) {
        if (err) throw err;
        ...
    });
    

    Mainly because that’s the coding style used in the node community. If you’re not interacting with node.js at all then you should probably use the style most common to the environment you’re interacting with.

    If your environment mainly revolves around jQuery then I would just return a $.Deferred object from your doSomething function

    function doSomething(data, options) {
      return $.Deferred();
    }
    
    $.when(
      doSomething(data, { ... })
    ).then(
      function () { /* success */ },
      function () { /* error */ }
    );
    

    Edit:

    function doSomething(data, options, cb) {
      if (typeof options === "function") {
        cb = options;
        options = null;
      }
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is about Javascript. I have a Callback function which receives a Position
I have Java application which sends pointer to function (callback) to some native dll
I have the following code, in which Boost.Local uses a function callback to load
I have a js function which has, until now, always been the callback for
I have a callback function which has the parameter const unsigned char *pData .
I have an onclick function which performs several tasks. In another JavaScript function I
I have a callback function within an ASP.NET Web Forms application which I am
Here I create a class in JAVA in which I have function (callback) which
I am trying to figure out exactly how to implement a callback function which
PHP functions such as 'array_map' take a callback, which can be a simple function

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.