Created
January 23, 2012 17:23
-
-
Save armyofda12mnkeys/1664355 to your computer and use it in GitHub Desktop.
process.nextTick example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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