javascript - Node JS event not firing -


this question has answer here:

i trying make event fire in node js code cannot work out why isn't.

i have module follows:

var zookeeper = require('node-zookeeper-client'),     events = require('events'),     util = require('util'); var client;  var kafkazookeepermonitor = function(){     events.eventemitter.call(this);   }  util.inherits(kafkazookeepermonitor, events.eventemitter);  kafkazookeepermonitor.prototype.connecttozk = function(zookeepers){     client = zookeeper.createclient(zookeepers);     client.connect();      client.once('connected', function () {         console.log('connected zookeeper');         this.emit('connected');     }); }  module.exports = kafkazookeepermonitor; 

and call index.js file follows:

var kafkazookeepermonitor = require('./kafkazookeepermonitor'),     kafkastatus = new kafkazookeepermonitor(),     brokerpath = '/brokers/ids';  kafkastatus.connecttozk('192.168.50.252:2181');  kafkastatus.once('connected', function(){     console.log('watcher connected zookeeper'); }) 

when run code log message

'connected zookeeper'

but this.emit('connected'); not seem fire.

i've looked around on web , think i'm setting event emitter correctly. can me out might going wrong here?

thanks

the context of callback function not kafkazookeepermonitorz object this not object instance.

this should work:

kafkazookeepermonitor.prototype.connecttozk = function(zookeepers){   var self = this;   client = zookeeper.createclient(zookeepers);   client.connect();    client.once('connected', function () {       console.log('connected zookeeper');       self.emit('connected');   }); } 

the reason it's not throwing error may because client extending eventemitter


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -