The following program will hang in NodeJS, does anybody know why?
ended = false;
events = require('events');
eventEmitter = new events.EventEmitter();
eventEmitter.on('end', function() {
ended = true;
});
setTimeout(function() {
eventEmitter.emit('end');
}, 100);
while (!ended) {
process.nextTick();
}
console.log('ended');
nextTickisn’t some kind of yield operation, it’s for scheduling a callback to be called the next time the engine is free to do so. It’s “hanging” because thewhileloop’s exit condition is never satisfied (and can never be, with that code).