最佳答案
我想知道配置模块导出的最佳方法是什么。下面的示例中的“ sync.function”可以是 FS 或 HTTP 请求,为了示例而简化:
下面是示例代码(synmodule.js) :
var foo = "bar"
async.function(function(response) {
foo = "foobar";
// module.exports = foo; // having the export here breaks the app: foo is always undefined.
});
// having the export here results in working code, but without the variable being set.
module.exports = foo;
如何只在执行了异步回调之后才导出模块?
关于我的实际用例的一个简短说明: 我正在编写一个模块,用于在 fs.exis ()回调中配置 nconf (https://github.com/flatiron/nconf)(即它将解析一个配置文件并设置 nconf)。