
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
asyncevents
Advanced tools
Extension to EventEmitter to facilitate asynchronous firing of events, and asynchronous handling of those events.
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).
... 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.
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();
fs.readFile(someReferenceToDocumentRoot + url.parse(req.url).pathname, function(err, data) {
// Invoking the callback with an error will immediately stop any further handler execution, and call the emit callback with the exception.
if(err) return callback(err);
req.responder = function(res) {
res.writeHead(200 /* http headers here*/);
res.end(data);
};
// Invoking the callback with true ensures no further handlers are executed.
callback(true);
});
});
});
** A synchronous event consumer: helloworld.js ** requestModule.on("request", function(req) { var handled = false; if(/.hello$/.test(req.url)) { req.responder = function(res) { res.writeHead(200 /* http headers here*/); res.end("Hello, world!"); }; handled = true; }
// Returning true will ensure no further handlers in the chain are executed.
return handled;
});
Could achieve the same result in a way, yes. However asynchronous event handling is more specific to .... handling event emitting in an asynchronous manner. Also, using this method gives you a formal way to terminate an event in much the same way as you would in the W3C event model. Finally, this model supports handlers being both asynchronous and synchronous.
Probably a few. If you find anything please let me know, or fix it yourself and push the changes to me.
One issue is if handler is deferred, and the resulting callback is not invoked, then the emit() callback will never be called. This could potentially leave HTTP requests/whatever in an eternal wait. Perhaps a timeout should be worked in somewhere.
FAQs
Extension to EventEmitter to facilitate asynchronous firing of events, and asynchronous handling of those events.
The npm package asyncevents receives a total of 1 weekly downloads. As such, asyncevents popularity was classified as not popular.
We found that asyncevents demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.