Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
Solving circular dependency using Events - Node.js

I know circular dependency is considered to be a design issue, but what if it's not possible to redesign the program, Is it a bad practice to solve it with events?

For example, let's say I have two classes I want to call methodB from ClassA and methodA from ClassB.

The solution looks like this:


class ClassA {

    classB: ClassB;

    constructor() {
        this.classB = new ClassB();
        this.classB.methodB();

        this.classB.eventEmitter.on('callMethodA', (data) => {
            this.methodA(data);
        });
    }

    methodA(data) {
        console.log(data);
    }
}

class ClassB {

    eventEmitter: EventEmitter;

    constructor() {
        this.eventEmitter = new events.EventEmitter();
        this.eventEmitter.emit('callMethodA', 'some data');
    }

    methodB() {
        console.log("hello");
    }

}

There are 2 downsides I can think of:

  1. ClassB can't get the return value from methods in ClassA.
  2. ClassA must be Singelton to avoid duplicate listeners.

Is it considered to be a bad practice? What are the alternatives?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!