voltrevo-event-loop
v0.1.0
Published
An event loop abstraction.
Downloads
3
Readme
voltrevo-event-loop
An event loop abstraction.
Install
$ npm install --save voltrevo-event-loop
Usage
At the moment, this package basically implements setTimeout
without using the global event loop. Control is given to you in the form of .run()
and .runNext()
.
var EventLoop = require('voltrevo-event-loop');
var el = EventLoop();
el.post(function() {
console.log('a');
}, 100);
el.post(function() {
console.log('b');
});
el.post(function() {
console.log('c');
});
// No output yet
el.runNext(); // b
el.runNext(); // c
el.runNext(); // a
// Or use el.run() to keep running until no events are left.
.run()
will run all tasks that it can possibly see, which includes tasks that get added during .run()
. It doesn't just run the tasks that have been posted before the call.
el.post(function() {
el.post(function() {
console.log('a');
});
});
el.post(function() {
console.log('b');
});
el.post(function() {
console.log('c');
});
// No output yet
el.run(); // b, c, a
License
MIT © Andrew Morris