I am playing around with node/javascript. This is just a simple code, logic is not important just playing with the EventEmitter. Can someone please tell me what I am doing wrong in terms of code not the logic.
Thanks for the help.
var emitter = require('events').EventEmitter;
var http = require('http');
function github()
{
var emitterInstance = new emitter();
this.getEmitter = function()
{
console.log('returning the emitterInstance');
return emitterInstance;
};
this.getData = function()
{
http.get("https://api.github.com/users/loneshark99/gists", function(error,resp)
{
if(error)
{
console.log('Error occured!');
}
else
{
emitterInstance.emit('dataReceived');
}
});
};
return {
emt : getEmitter,
getData : getData
};
}
var g = new github();
g.emt().on("dataReceived", function() { console.log('data received from github')})
g.getData();
Error is :
D:\test.js:33
getData : getData
^
SyntaxError: Unexpected token :
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Answer :: This works
var emitter = require('events').EventEmitter;
var http = require('http');
function github()
{
var emitterInstance = new emitter();
this.getEmitter = function()
{
console.log('returning the emitterInstance');
return emitterInstance;
};
this.getData = function()
{
http.get("http://pluralsight.com", function(resp)
{
emitterInstance.emit('dataReceived');
});
};
var obj = { emt : getEmitter, getData : getData };
return obj;
}
var g = github();
console.log(g);
g.emt().on("dataReceived", function() { console.log('data received from github')})
g.getData();