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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:32:37+00:00 2026-06-15T08:32:37+00:00

In class which inherits EventEmitter, we usually have two points to handle error. someClass.on(‘error’,

  • 0

In class which inherits EventEmitter, we usually have two points to handle error.

someClass.on('error', function eventEmitterCallback(err){/* handle error*/});
//and
someClass.someAsyncAction(function asyncActionCallback(err){/* handle error*/})

We don’t know, where error can appear, and what callback will execute.
For example, tedious (module for MS SQL Server):

  • Incorrect password: both callbacks
  • Incorrect database name: only eventEmitterCallback; someAsyncAction execute without error
  • Incorrect host: only someAsyncAction
  • Icorrect format of host IP(for example 1234.10.0.1): node.js just down without any error 🙂

I want to create a simple function that will deligate all errors from module to first argument of callback.

function getSomeStuffFromDb(callback) { 
    var connection = new dbConnection(options);
    connection.on('error', callback);
    connection.connect(function(err) {
        if (err) return callback(err);
        /*exec SQL query*/
    }
}

Of course, if any error cause, I want to execute callback only once.How can I make this? How correctly union two points of error handling in one? Maybe if in my class I use class that inherits EventEmmiter, I need enherits my class from EventEmitter too.

  • 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-15T08:32:39+00:00Added an answer on June 15, 2026 at 8:32 am

    Use the once event binding if you want to avoid running the same event handler over and over. Given your code sample, I don’t believe once and on will behave any differently because the connection object is not long-lived, but it communicates the intention a touch more clearly. Then use a closure-scope flag to avoid both the event handler and callback responding to the same underlying error.

    function getSomeStuffFromDb(callback) { 
        var handled = false;
        var connection = new dbConnection(options);
        connection.once('error', function (err) {
            if (err && !handled) {
                handled = true;
                return callback(err);
            }
        });
        connection.connect(function(err) {
            if (err) {
              if (handled) {
                return;
              } else {
                handled = true;
                return callback(err);
              }
            }
            /*exec SQL query*/
            callback();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, one which inherits the other, for example:- public class A
I have a class which inherits from Canvas. On the OnRender method I draw
I have a form builder class which inherits from AbstractType and I need to
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have my class X which inherits from Qt's class Base . I declared
I have a class B which inherits from A which in turn derives from
I have copied class from net which inherits UIImageView. How to put that class
I have a class which inherits from std::runtime_error like so: #include <string> #include <stdexcept>
I have a class which inherits from CListCtrl class, say class list. I have
I have an abstract base class which inherits Sharp Arch's Entity class: /// <summary>

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.