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

  • Home
  • SEARCH
  • 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 4616884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:02:45+00:00 2026-05-22T02:02:45+00:00

I am writing a javascript function that takes a callback. The callback will be

  • 0

I am writing a javascript function that takes a callback. The callback will be passed an error argument if something goes wrong.

What are the best / most standard calling conventions?

  • Should the callback’s error argument be first or last?
  • Should I pass an 'errorMsg' string, or a new Error('errorMsg') object?

Ie, what is more correct – this code:

foo = function(bar, callback) {
  ...
  if (error) {
    callback('troz not found');
  } else {
    callback(null, result);
  }
}

or this code:

foo = function(bar, callback) {
  ...
  if (error) {
    callback(null, 'troz not found');
  } else {
    callback(result);
  }
}

or this:

foo = function(bar, callback) {
  ...
  if (error) {
    callback(null, new Error('troz not found'));
  } else {
    callback(result);
  }
}

If its relevant, my code will be used as both as a NodeJS module and as a browser-based javascript library.

  • 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-22T02:02:46+00:00Added an answer on May 22, 2026 at 2:02 am

    You should choose a convention for your library and stick with it; otherwise, it is arbitrary. There are many different ways of handling errors in JavaScript:

    1. Taking both a “success” and “error” callback.
    2. Taking a single callback to handle both “success” and “error” cases.
    3. Having a registration mechanism for a default “error” handler, making error handlers optional on all other calls, using the immediate or fallback error callbacks as appropriate.

    Combined with:

    1. Using no parameters to the error callback.
    2. Using a boolean to indicate success / failure.
    3. Using a string to encapsulate the nature of the error.
    4. Using some more complex “Result” object to encapsulate success/failure and the result.
    5. Using some complex object to encapsulate detailed failure information.

    Really, it’s entirely up to you. It also depends on what you are trying to do. For example, if you don’t really intend to do anything with the failure information, then it might not be worth the expense of constructing / passing around that extra information. But perhaps you have lots of detailed failure information you want to pass to the user or back to the API caller, in which case it makes lots of sense.

    Choose a convention, and stick with it. Other than that, it’s entirely up to you.

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

Sidebar

Related Questions

I'm writing a piece of client-side javascript code that takes a function and finds
I'm writing a Javascript function that would manipulate an array written on-the-fly and sent
I'm writing a JavaSCript class that has a method that recursively calls itself. Scheduler.prototype.updateTimer
In Notepad++, I was writing a JavaScript file and something didn't work: an alert
I am writing controls that work nice with JavaScript, but they have to work
I have problem in some JavaScript that I am writing where the Switch statement
I am writing a JavaScript function to highlight the row and column of a
I'm writing a quite complex JavaScript application that has an MVC architecture that I'm
I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed
I'm writing some Javascript to resize the large image to fit into the user's

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.