진성욱

Chapter08 Added

function add(a,b) {
return a+b;
}
function sub(a,b) {
return a-b;
}
var Producer = require('./producer');
var producer = new Producer();
producer.on('add', function(t) {
console.log('add:', t);
});
producer.start();
// stop the clock 10 seconds after
// setTimeout(function() {
// clock.stop();
// }, 10e3)
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var Producer = function () {
if (! (this instanceof Producer)) return new Producer();
EventEmitter.call(this);
}
module.exports = Producer;
var fs = require("fs");
var searchString = "test";
var events = require('events');
var eventEmitter = new events.EventEmitter();
var connectHandler = function connected() {
console.log('connection successful.');
eventEmitter.emit('data_received');
}
eventEmitter.on('connection', connectHandler);
eventEmitter.on('data_received', function(){
console.log('data received successfully.');
});
eventEmitter.emit('connection');
console.log("Program Ended.");
var events = require('events');
var eventEmitter = new events.EventEmitter();
var listner1 = function listner1() {
console.log('listner1 executed.');
}
var listner2 = function listner2() {
console.log('listner2 executed.');
}
eventEmitter.addListener('connection', listner1);
eventEmitter.on('connection', listner2);
var eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
eventEmitter.emit('connection');
eventEmitter.removeListener('connection', listner1);
console.log("Listner1 will not listen now.");
eventEmitter.emit('connection');
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
console.log("Program Ended.");
var dt = require('./mymodule');
console.log(dt.myDateTime());
exports.myDateTime = function () {
return Date();
};
No preview for this file type
No preview for this file type