Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda