Created
February 6, 2013 16:54
-
-
Save mariusbutuc/4723964 to your computer and use it in GitHub Desktop.
How Marius understood Node's single threaded asynchronous-ness & events
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 events = require('events'); | |
var eventEmitter = new events.EventEmitter() | |
function mainLoop() { | |
console.log('starting application'); | |
eventEmitter.emit('AppStart'); | |
console.log('Running App'); | |
eventEmitter.emit('AppRun'); | |
console.log('Stopping App'); | |
eventEmitter.emit('AppStop'); | |
} | |
function onAppStart() { | |
console.log('»1 Handling App start Event'); | |
} | |
function onAppRun() { | |
console.log('»2 Handling App run Event'); | |
setTimeout(function() { | |
console.log('»3 » Marius runs here!'); | |
}, 5000); | |
} | |
function onAppStop() { | |
console.log('»4 Handling App Stop event'); | |
} | |
eventEmitter.on('AppStart', onAppStart); | |
eventEmitter.on('AppRun', onAppRun); | |
eventEmitter.on('AppStop', onAppStop); | |
function onAppStart () { | |
console.log('»1 » » start, start, start!!!'); | |
} | |
mainLoop(); |
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
[node-network (master)]$ node events.js | |
Starting application | |
»1 » » Start: go, go, go!!! | |
Running Application | |
»2 Handling Application run Event | |
Stopping Application | |
»4 Handling Application Stop event | |
»3 » Marius runs here! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment