我有一组文件,module1.js、module2.js、module3.js,每个文件都包含一个返回对象,该对象具有将要执行的属性方法。检查对象以动态确定属性名称,并且我可以.toString()
方法。
内部方法几乎肯定会包含异步调用,如下所示:
function doSomething(vars){
var that = this,
red = 'blue',
up = 'down',
one = 2;
makeAsync(that,red,up,one,function(err,res){
return makeDeep(res);
}
}
我如何从调用父方法整理这些方法,以最终返回值,而不主动对文件module1.js、module2.js和module3.js具有可写访问权限。假设这些都是石头铸的,永远无法编辑。任何其他合理的事情都是公平的。只是请不要说"好吧,重写doSomething以传递CB,并让makeDeep
封装在CB中"。请注意,我是从自己的代码中调用这个模块的,并注意makeAsync
是模块作者想要调用的任何异步方法。
重要提示:我是编写makeDeep
的人,我是包括模块的人,所以我可以在这两个地方做任何你想做的事情,并且makeDeep
是动态注入到模块中的(我正在执行混合模式),所以如果你的解决方案依赖于修改makeDeep
以使其工作或"父"调用方法中的某些东西,那是100%合理的,我完全同意。
如果是这种情况,则不需要在makeDeep
之前有return
关键字,但如果语法确实使用了这些单词,则会获得加分(这向开发人员严重表明这是代码退出点,是吗?)
假设module1.js看起来像:
module.exports = function() {
this.doSomething11 = function doSomething(vars){
var that = this,
red = 'blue',
up = 'down',
one = 2;
makeAsync(that,red,up,one,function(err,res){
return makeDeep(res);
}
}
}
模块2.js
module.exports = function() {
this.doSomething21 = function doSomething(vars){
var that = this,
red = 'blue',
up = 'down',
one = 2;
makeAsync(that,red,up,one,function(err,res){
return makeDeep(res);
}
};
this.doSomething22 = function doSomething(vars){
var that = this,
red = 'blue',
up = 'down',
one = 2;
makeAsync(that,red,up,one,function(err,res){
return makeDeep(res);
}
};
}
模块3.js
module.exports = function() {
this.doSomething31 = function doSomething(vars){
var that = this,
red = 'blue',
up = 'down',
one = 2;
makeAsync(that,red,up,one,function(err,res){
return makeDeep(res);
}
};
this.doSomething32 = function doSomething(vars){
var that = this,
red = 'blue',
up = 'down',
one = 2;
makeAsync(that,red,up,one,function(err,res){
return makeDeep(res);
}
};
this.doSomething33 = function doSomething(vars){
var that = this,
red = 'blue',
up = 'down',
one = 2;
makeAsync(that,red,up,one,function(err,res){
return makeDeep(res);
}
}
}
是的,这些例子是人为的,因为我更关注概念,而不是实际的细节。它们可以是三重嵌套回调,也可以使用某种内部回调。大多数情况下,我只是想知道是否有办法做到这一点。
如果没有,如果我为用户提供一个特定的库并让他们返回库,我如何让它为他们工作?
我的目标是最终复制类似于ASP.NET风格ActionResult的东西,我对使用Q、fiber或promise的想法持开放态度,但在使用异步回调时,我在调用中缺少了一些东西来将其恢复。
我将尝试解决您的问题,该问题只需要所需的模块调用return that.makeDeep()
,而不仅仅是return makeDeep()
。我知道你不想更改被调用的代码,但嘿,你总是可以使用burrito并动态更改这些行(不需要写访问权限)。
调用代码
var Controller = require('./module1');
assert(typeof Controller == 'function');
httpServer.on('request', function(req, res) {
// I assume this is something you would do
var vars = processReqParameters(req);
var action = vars.action;
var controller = new Controller();
if(typeof controller[action] === 'function') {
// now I assume that makeDeep will at one point
// call res.end();
var makeDeep = createMakeDeep(req, res, vars) // or other parameters
// this is how we inject makeDeep in the controller
var controllerInstance = Object.create(controller, {makeDeep: makeDeep});
return controllerInstance[action](vars);
}
else {
res.writeHead(404, 'Controller Not Found');
res.end('Too badn');
}
})
调用代码
module.exports = function() {
this.myAction = function(vars) {
var that = this,
red = 'blue',
up = 'down',
one = 2;
makeAsync(that, red, up, one, function(err, res) {
return that.makeDeep(res);
})
}
}