Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda