Skip to content

Instantly share code, notes, and snippets.

@rehanift
Created December 10, 2011 17:12
Show Gist options
  • Save rehanift/1455618 to your computer and use it in GitHub Desktop.
Save rehanift/1455618 to your computer and use it in GitHub Desktop.
Node VM waiting for event listeners to fire
var vm = require("vm"),
util = require("util"),
events = require("events");
var fooClass = function(){};
util.inherits(fooClass, events.EventEmitter);
fooClass.prototype.run = function(){
this.emit('myevent', "Hello!");
};
var context = {
Foo: fooClass,
console: console
};
var code = "var foo = new Foo(); foo.on('myevent', function(data){ console.log(data); }); foo.run();";
vm.runInNewContext(code, context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment