Created
August 14, 2012 11:19
-
-
Save thatguydan/3348294 to your computer and use it in GitHub Desktop.
nodejs control flow management
This file contains 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; | |
function User() { | |
var self = this; | |
this.create = function(email,password) { | |
self.once('_insertIntoDbEvent',function(err) { | |
/* | |
Step 2 DONE | |
All Finished | |
*/ | |
self.emit('UserCreatedEvent',err); // Go home | |
}); | |
self.once('_validateInputsEvent',function(err) { | |
/* | |
Step 1 DONE | |
*/ | |
self._insertIntoDb(email,password); // Step 2 | |
}); | |
self._validateInputs(email,password); // Step 1 | |
}; | |
this._validateInputs = function(email,password) { | |
// ... | |
self.emit('_validateInputsEvent',err) | |
}; | |
this._insertIntoDb = function(email,password) { | |
// ... | |
self.emit('_insertIntoDbEvent',err) | |
}; | |
}; | |
User.prototype = new EventEmitter; | |
exports.User = User; |
This file contains 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 User = require('model.js').User; | |
exports.createUser = function(req,res) { | |
var user = new User(); // Instantiate model | |
user.once('UserCreatedEvent',function(err) { | |
// ... | |
user = function(){}; // Cleanup this instance | |
}); | |
user.create(email,password); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment