gpt4 book ai didi

node.js - 如何在没有文件写入权限的情况下收集一个可能包含它自己的异步调用的方法?

转载 作者:搜寻专家 更新时间:2023-11-01 00:02:54 25 4
gpt4 key购买 nike

我有一组文件,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 被动态注入(inject)到模块中(我正在做一个混合模式)所以如果你的解决方案依赖于修改 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);
}
}
}

module2.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);
}
};
}

module3.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、fibers 或 promises 的想法持开放态度,但我在调用周围遗漏了一些东西,以便在使用异步回调。

最佳答案

我会解决你的问题,它只需要所需的模块调用 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 bad\n');
}
})

调用代码

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);
})
}
}

关于node.js - 如何在没有文件写入权限的情况下收集一个可能包含它自己的异步调用的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14777916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com