Sunday, 15 January 2012

node.js - How can I corral a method which may contain it's own async calls without having write access to the file? -



node.js - How can I corral a method which may contain it's own async calls without having write access to the file? -

i have set of files, module1.js, module2.js, module3.js, , each of these contains homecoming object has property-methods executed. objects inspected determine property names dynamically, , can .tostring() methods.

the inner methods contain async calls, this:

function dosomething(vars){ var = this, reddish = 'blue', = 'down', 1 = 2; makeasync(that,red,up,one,function(err,res){ homecoming makedeep(res); } }

how can marshall these methods calling parent method homecoming value without actively having writeable access files module1.js, module2.js , module3.js. assume cast in stone, , can never edited. else that's reasonable fair game. please don't "well, rewrite dosomething pass in cb , allow makedeep wrapped in cb". note i'm calling module own code, , note makeasync whatever asynchronous methods module-author wants call.

important note: 1 writing makedeep , 1 including module can whatever want in either of 2 places, , makedeep injected module dynamically (i'm doing mixin pattern) if solution relies on modifying makedeep work or in "parent" calling method, 100% reasonable , i'm that.

if case, there's no "need" have return keyword before makedeep bonus points if syntax utilize words (that heavily indicates developer that's code exit point, yes?)

assume module1.js looks like:

module.exports = function() { this.dosomething11 = function dosomething(vars){ var = this, reddish = 'blue', = 'down', 1 = 2; makeasync(that,red,up,one,function(err,res){ homecoming makedeep(res); } } }

module2.js

module.exports = function() { this.dosomething21 = function dosomething(vars){ var = this, reddish = 'blue', = 'down', 1 = 2; makeasync(that,red,up,one,function(err,res){ homecoming makedeep(res); } }; this.dosomething22 = function dosomething(vars){ var = this, reddish = 'blue', = 'down', 1 = 2; makeasync(that,red,up,one,function(err,res){ homecoming makedeep(res); } }; }

module3.js

module.exports = function() { this.dosomething31 = function dosomething(vars){ var = this, reddish = 'blue', = 'down', 1 = 2; makeasync(that,red,up,one,function(err,res){ homecoming makedeep(res); } }; this.dosomething32 = function dosomething(vars){ var = this, reddish = 'blue', = 'down', 1 = 2; makeasync(that,red,up,one,function(err,res){ homecoming makedeep(res); } }; this.dosomething33 = function dosomething(vars){ var = this, reddish = 'blue', = 'down', 1 = 2; makeasync(that,red,up,one,function(err,res){ homecoming makedeep(res); } } }

yes, examples contrived, because i'm more focused on concept on actual specifics. triply nested callbacks, or utilize sort of internal callbacks. want know if there's way create happen.

if not, how can create work users if provide them specific library , have them homecoming library?

my goal end replicating similar asp.net style actionresult, , i'm open thought of using q, fibers or promises, i'm missing around invocation when async callback used.

i'll have go @ problem requires required module phone call return that.makedeep() instead of return makedeep(). know did not want alter called code, hey, can utilize burrito , alter lines dynamically (no write access needed).

calling code

var controller = require('./module1'); assert(typeof controller == 'function'); httpserver.on('request', function(req, res) { // assume var vars = processreqparameters(req); var action = vars.action; var controller = new controller(); if(typeof controller[action] === 'function') { // assume makedeep @ 1 point // phone call res.end(); var makedeep = createmakedeep(req, res, vars) // or other parameters // how inject makedeep in controller var controllerinstance = object.create(controller, {makedeep: makedeep}); homecoming controllerinstance[action](vars); } else { res.writehead(404, 'controller not found'); res.end('too bad\n'); } })

called code

module.exports = function() { this.myaction = function(vars) { var = this, reddish = 'blue', = 'down', 1 = 2; makeasync(that, red, up, one, function(err, res) { homecoming that.makedeep(res); }) } }

node.js asynchronous

No comments:

Post a Comment