Skip to content

Instantly share code, notes, and snippets.

@mlaccetti
Created October 24, 2012 16:33
Show Gist options
  • Save mlaccetti/3947177 to your computer and use it in GitHub Desktop.
Save mlaccetti/3947177 to your computer and use it in GitHub Desktop.
Function vs. Events
//
// functional
//
http.request(url, function(req, res) {
var html = '';
res.on('data', function(chunk) {
html += chunk;
});
res.on('end', function() {
var elements = html.match(/findMe/g);
_.each(elements, function(element) {
// keep going
});
});
});
//
// event emitter
//
var emit = new EventEmitter();
emit.on('scrape', function(url, nextStep) {
http.request(url, function(req, res) {
var html = '';
res.on('data', function(chunk) {
html += chunk;
});
res.on('end', function() {
emit.emit(nextStep, html);
});
});
});
emit.on('nextStep', function(html) {
var elements = html.match(/findMe/g);
_.each(elements, fnction(element) {
// keep going
});
});​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment