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

198
Visualizações
Get eventemitter messages from method of object Javascript

I want to get all progress and updates of loop from this object method. I have used eventEmitter to emit a message and then catch that signal with .on like this:

let response= site_ob.getAllProducts();
    response.on(('gotit'),(data)=>{
        console.log("data");
    });

But it shows response.on is not a function.

Below is a method of a class:

async getAllProducts(){
        const eventEmitter=new EventEmitter();
        let ended=false,products_array = [],count=0,promises_array = [];
        let total_products_num=this.getTotalProductNumbers();

        for(let i=0;i<=150;i+=50) {

              let products_data=  await this.getProducts(i, 50);
            products_array.push(products_data);
            eventEmitter.emit('gotit', products_data);
        }

        return products_array;
    }

Or can I use pipe or stream to do this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to have your class extend eventemitter and then call this.emit() in getAllProducts. As it stand your call the on function on an array as your return your products_array

class MyProductClass extends EventEmitter {

    constructor() {
        super();
    }

    async getAllProducts(){
        let ended=false,products_array = [],count=0,promises_array = [];
        let total_products_num=this.getTotalProductNumbers();

        for(let i=0;i<=150;i+=50) {
            let products_data =  await this.getProducts(i, 50);
            products_array.push(products_data);
        }
        
        this.emit('gotit', products_data);
    }

}

Then call it like this

let response;
site_ob.on( 'gotit', ( data ) => {
    response = data;
    console.log(response);
}

site_ob.getAllProducts();
about 4 years ago · Juan Pablo Isaza Relatório
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