I dont know what is the problem with my code.
// emitter.js
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function Loadfun(param1, param2, db){
function __error(error, row){
if(error){
this.emit('error', error);
return true;
}
if(row.length < 1)
this.emit('failure');
}
function doSomething(){
db.query('select something', callback);
}
function callback(err, result){
if(__error(error))
return false;
else
this.emit('success', result);
}
this.doSomething = doSomething;
};
util.inherits(Loadfun,EventEmitter);
module.exports = Loadfun;
This is the emitter function. and i am using this for some sync db works. the following is the calling function.
var emitter = require('emitter');
router('/fetch', function(req, res){
var fetch = new emitter(param1, param2, db);
fetch.on('failure', function(){
console.log('error');
});
fetch.on('success', function(data){
console.log(JSON.stringify(data));
});
fetch.doSomething();
});
this works perfectly fine without any errors. I tried logging the flow till the emiting of success but the catching of the event emitting is not getting logged.. I dont understand what is the problem.. It would be nice if someone could help.