Skip to content

Instantly share code, notes, and snippets.

@armyofda12mnkeys
Created January 23, 2012 17:23
Show Gist options
  • Save armyofda12mnkeys/1664355 to your computer and use it in GitHub Desktop.
Save armyofda12mnkeys/1664355 to your computer and use it in GitHub Desktop.
process.nextTick example
var http = require('http')
var MyConstructor = require('./myconstructor')
http.createServer(function(request, response) {
var c = new MyConstructor();
c.on('data', function(data) {
console.log(data);
});
c.on('end', function(){ console.log('end'); } );
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
var EventEmitter = require( "events" ).EventEmitter;
var MyConstructor = function() {
var self = this;
process.nextTick(function() {
self._continue();
});
};
MyConstructor.prototype.__proto__ = EventEmitter.prototype;
MyConstructor.prototype._continue = function() {
// without the process.nextTick
// these events would be emitted immediately
// with no listeners. they would be lost.
this.emit('data', 'hello');
this.emit('data', 'world');
this.emit('end');
};
module.exports = MyConstructor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment