[英]function in a function return value
import.js
exports.getConfig = function() {
return api.getConfig();
};
test.js
// Aanmaken lightBridge
obj = reflector.getObj();
console.log(obj);
// Toon alle lichten
obj.getConfig().then(function(config) {
console.log(config);
}).done();
在最后一个代码段中,它使用的功能是:当我调用getConfig()时,我希望输出的内容位于变量config中。 现在的问题是,当我想记录未定义的变量测试时。
如果我用console.log(config)而不是return config; 它完美地工作。 似乎很奇怪。
我想使用它时的输出结果就像varia.getConfig()=> config的输出。
测试仅存在于您的函数中,而不存在于外部。 您可以尝试这样的操作,但是可能很脏。
var test;
exports.getConfig = function() {
api.getConfig(function(err, config) {
if (err) throw err;
test = config;
});
听起来您正在尝试使用异步函数,就好像它是同步的一样。 你不能这样做。 您可以做的就是从getConfig
返回一个getConfig
。
exports.getConfig = function() {
return api.getConfig();
};
然后您的模块可以像这样被消耗:
const myModule = require('my-module');
myModule.getConfig().then(function(config) {
console.log(config);
});
从注释中听起来好像您正在使用Express。 如果要使用Express在HTTP响应中发送config
,则可以执行以下操作:
app.get('/config', function(request, response) {
myModule.getConfig().then(function(config) {
response.send(config);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.