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

142
Views
Avoid circular dependencies: replace import for events?

I'm splitting one file code into multiple ones and I run into a few circular dependencies.

Some of them, I am able to avoid them by refactoring a bit, others it seem more complicated or it just wouldn't make sense to me as the refactoring will probably mean having to put a lot of code together in the same file instead of splitting it by features/functionalities.

I've seen other components using events to communicate between modules and I thought I could use this same technique to avoid the circular dependency, but.... I'm not sure if this is just an ugly hack rather than the proper way of doing it.

So, for example, having the following:

import { sayHi } from 'introductions.js';

export function demo(){
    // stuff here
    
   sayHi(stuff); // circular dependency here 
}

I was thinking I could replace it for the following:

export function demo(){
    // do stuff here
    
   emit('sayHi', stuff); // event here, avoiding the import 
}
//introductions.js
import { whatever } from './whatever.js';

on('sayHi', function(stuff){
   sayHi(stuff);
});

function sayHi(stuff){
   console.log("Hi " + stuff);
}

...

Would this be a proper solution to it? Or just an ugly hack to "remove" the circular dependency?

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!