I am a novice JavaScripter and have no real knowledge of what goes on inside the V8 engine.
Having said that, I am really enjoying my early forays into the node.js environment but I find that I am constantly using events.EventEmitter() as a means to emit global events so that I can structure my programs to fit a notifier-observer pattern similar to what I would write in say an Objective-C or Python program.
I find myself always doing things like this:
var events = require('events');
var eventCenter = new events.EventEmitter();
eventCenter.on('init', function() {
var greeting = 'Hello World!';
console.log('We're in the init function!);
eventCenter.emit('secondFunction', greeting);
});
eventCenter.on('secondFunction', function(greeting) {
console.log('We're in the second function!);
console.log(greeting);
eventCenter.emit('nextFunction');
});
eventCenter.on('nextFunction', function {
/* do stuff */
});
eventCenter.emit('init');
So in effect I'm just structuring 'async' node.js code into code that does things in the order I expect, instead I'm kind of "coding backwards" if that makes sense. Would there be any difference in doing this in a callback-heavy manner, either performance-wise or philosophy-wise? Is it better to do the same thing using callbacks instead of events?