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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:08:01+00:00 2026-06-15T01:08:01+00:00

In my code I use a lot of (named) callback functions, just to give

  • 0

In my code I use a lot of (named) callback functions, just to give a quick example:

function showThis(callback) {

  // Do something

  if (callback && typeof(callback) === 'function') {
    callback();
  }

}

Now I have this pattern repeat throughout different functions (I’m talking about the callback part), so is it considered to be better if I make one generic callback handler function and include that?

Something like:

function doCallback(callback) {

  if (callback && typeof(callback) === 'function') {
    callback();
  }

}

function showThis(callback) {

  // Do something

  doCallback(callback);
}

I would think that’s better to keep code DRY, but I’m unsure. Any help is greatly appreciated!

  • 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-15T01:08:02+00:00Added an answer on June 15, 2026 at 1:08 am

    In my opinion it’s not necessary to have a callback handler. In different situations you may need to call the callback with different context (for example callback.call(ob... or callback.apply(obj...). So you will need an extra parameter of the callback handler (the context). Another thing that isn’t very pleasantly is that you may need to pass custom arguments to the callback. With the callback handler you can make the pain smaller by passing all the parameters into an array and applying the function on them. Something like…:

    function callbackHandler(callback, arguments, context) {
        if (typeof callback === 'function') {
            return callback.apply(context, arguments);
        }
        return null;
    }
    

    But there’re so many optional parameters…
    Another thing is the check which you are doing:

    if (callback && typeof callback === 'function') //notice that typeof is an operator not a function, so you don't need parentless
    

    is not actually necessary, this is enough: typeof callback === 'function'

    The first condition will return false only if callback is evaluated to false but if it’s evaluated to false typeof callback wont return function. So the condition is short enough.

    That’s why I think that you don’t need a callback handler. And by the way nice question I love such topics! 🙂

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

Sidebar

Related Questions

I have the following code: use strict; function isDefined(variable) { return (typeof (window[variable]) ===
Using the following code: use LWP::Simple; my $url= http://example.com; my $html= get $url; I
The original Code: 'use strict'; function GitJs(config) { var defaults = { inheriting: false,
I know this code use to work, I must have accidentally changed something when
I have the following code: use Imager::Screenshot 'screenshot'; my $img = screenshot(hwnd => 'active',
I have the following test code use Data::Dumper; my $hash = { foo =>
I use Eclipse to write Java code and use DropBox to sync my code
My code i use to parse HTMl is this below, and the 2nd code
Here is a code I use : request.jsf <h:form> <!-- form inputs ... -->
I want to use code beside files for my views in my ASP.NET MVC

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.