Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 10, 2015 11:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/4430029 to your computer and use it in GitHub Desktop.
Save creationix/4430029 to your computer and use it in GitHub Desktop.
Example usage of runonce
var runOnce = require('uvrun').runOnce;
// Do something here, like make a server to keep the event loop busy
var TCP = process.binding('tcp_wrap').TCP;
var server = new TCP();
server.onconnection = function () {
console.log("connection!");
};
server.bind("0.0.0.0", 3000);
server.listen(511);
// Visualize each event loop tick using a custom event loop.
console.log("Waiting for events...");
do {
var ret = runOnce();
console.log("tick", Date.now());
} while(ret);
// If the code gets here, there are no events left and node's built-in uv_run won't block.
@mantoni
Copy link

mantoni commented Jan 2, 2013

Interesting way to achieve "parallel" execution. Is that any different from process.nextTick regarding performance?

@creationix
Copy link
Author

It's not parallel, it's just a new event loop that blocks inside the built-in one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment