<commit_before># Asynchronous Event Emitter

## What?

An augmented version of the standard EventEmitter that exposes an additional function: emitAsync.

emitAsync will fire each handler in a non-parallel manner (each handler has to finish before the next will be fired).

## Why?!

... Because I can!

Seriously though, use this if you want to handle events, but don't want execution to continue until all handlers have completed their (possibly) asynchronous handler logic.

## How?

Observe, a different approach to http middleware.

** An event producer: httpServer.js **
	var RequestHandler = function(req, res) {
		// ... a request has come in.
		// We want to let all interested parties know the a request has come in, and let one of them register a method to respond to the event.
		this.emitAsync("request", function(err) {
			if(!err) req.responder(res);
			else response500(err);
		}, req, res);
	}
	
	util.inherits(RequestHandler, AsyncEventEmitter);
	
	var requestModule = new RequestHandler();
	var httpServer = require("http").createServer();
	httpServer.listen(80);


** An async event consumer: staticFile.js **
	requestModule.on("request", function(req) {
		// Gonna need to see if the file exists locally.
		var callback = this.deferHandler();
		
		path.exists(someReferenceToDocumentRoot + url.parse(req.url).pathname, function(exists) {
			// Invoking callback() with no arguments (or false, null, undefined) will transfer control to next handler.
			if(!exists) return callback();<commit_msg>[fix] path.exists was moved to fs.exists<commit_after># Asynchronous Event Emitter

## What?

An augmented version of the standard EventEmitter that exposes an additional function: emitAsync.

emitAsync will fire each handler in a non-parallel manner (each handler has to finish before the next will be fired).

## Why?!

... Because I can!

Seriously though, use this if you want to handle events, but don't want execution to continue until all handlers have completed their (possibly) asynchronous handler logic.

## How?

Observe, a different approach to http middleware.

** An event producer: httpServer.js **
	var RequestHandler = function(req, res) {
		// ... a request has come in.
		// We want to let all interested parties know the a request has come in, and let one of them register a method to respond to the event.
		this.emitAsync("request", function(err) {
			if(!err) req.responder(res);
			else response500(err);
		}, req, res);
	}
	
	util.inherits(RequestHandler, AsyncEventEmitter);
	
	var requestModule = new RequestHandler();
	var httpServer = require("http").createServer();
	httpServer.listen(80);


** An async event consumer: staticFile.js **
	requestModule.on("request", function(req) {
		// Gonna need to see if the file exists locally.
		var callback = this.deferHandler();
		
		fs.exists(someReferenceToDocumentRoot + url.parse(req.url).pathname, function(exists) {
			// Invoking callback() with no arguments (or false, null, undefined) will transfer control to next handler.
			if(!exists) return callback();
{'fraction_non_alphanumeric': 0.09826400262037341, 'fraction_numerical': 0.003275466754012447, 'mean_word_length': 4.302083333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7598513', 'n_tokens_mistral': 884, 'n_tokens_neox': 818, 'n_words': 354}