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

195
Views
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 answers
Answer question

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 Report
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!