진성욱

Chapter08 Added

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