Skip to content

Instantly share code, notes, and snippets.

@loonison123
Created November 1, 2014 23:10
Show Gist options
  • Select an option

  • Save loonison123/0809f0e0fb09870dc791 to your computer and use it in GitHub Desktop.

Select an option

Save loonison123/0809f0e0fb09870dc791 to your computer and use it in GitHub Desktop.
Node.js emit events by inheriting EventEmitter
// csvHelper.js
var events = require('events');
function CsvHelper () {
this.config = null;
this.data = null;
// Need to instantiate EventEmitter constructor
events.EventEmitter.call(this);
}
// Copy event functions to this prototype
CsvHelper.prototype.__proto__ = events.EventEmitter.prototype;
// Custom prototyped functions
CsvHelper.prototype.parse = function () {
// Parse csv data into usable objects
if (parseSuccessful) {
this.emit('csvHelper.parseSuccessful', data);
} else {
this.emit('csvHelper.parseFailed', err);
}
}
CsvHelper.prototype.getHeaders = function () {
}
// Use the data
var csvHelper = new CsvHelper();
csvHelper.on('csvHelper.parseSuccessful', function (data) {
// Tell the user it parsed successfully
});
csvHelper.parse();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment