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?
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();