异步 nodejs 模块导出 [英] Asynchronous nodejs module exports
问题描述
我想知道配置模块导出的最佳方法是什么.下面示例中的async.function"可以是 FS 或 HTTP 请求,为了示例而简化:
I was wondering what the best approach is for configuring a module export. "async.function" in the example below could be a FS or HTTP request, simplified for the sake of the example:
这是示例代码(asynmodule.js):
Here's example code (asynmodule.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;
如何仅在执行异步回调后才导出模块?
How can I export the module only once the async callback has been executed?
编辑关于我的实际用例的快速说明:我正在编写一个模块来配置 nconf (https://github.com/flatiron/nconf) 在 fs.exists() 回调中(即它将解析配置文件并设置 nconf).
edit a quick note on my actual use-case: I'm writing a module to configure nconf (https://github.com/flatiron/nconf) in an fs.exists() callback (i.e. it will parse a config file and set up nconf).
推荐答案
您的导出无法工作,因为它在函数外而 foo
声明在函数内.但是如果你把导出放在里面,当你使用你的模块时,你不能确定导出是否被定义.
Your export can't work because it is outside the function while the foo
declaration is inside. But if you put the export inside, when you use your module you can't be sure the export was defined.
使用异步系统的最佳方式是使用回调.您需要导出一个回调分配方法来获取回调,并在异步执行时调用它.
The best way to work with an ansync system is to use callback. You need to export a callback assignation method to get the callback, and call it on the async execution.
示例:
var foo, callback;
async.function(function(response) {
foo = "foobar";
if( typeof callback == 'function' ){
callback(foo);
}
});
module.exports = function(cb){
if(typeof foo != 'undefined'){
cb(foo); // If foo is already define, I don't wait.
} else {
callback = cb;
}
}
这里的 async.function
只是一个占位符来象征一个异步调用.
Here async.function
is just a placeholder to symbolise an async call.
主要内容
var fooMod = require('./foo.js');
fooMod(function(foo){
//Here code using foo;
});
多种回调方式
如果你的模块需要被多次调用,你需要管理一个回调数组:
Multiple callback way
If your module need to be called more than once you need to manage an array of callback:
var foo, callbackList = [];
async.function(function(response) {
foo = "foobar";
// You can use all other form of array walk.
for(var i = 0; i < callbackList.length; i++){
callbackList[i](foo)
}
});
module.exports = function(cb){
if(typeof foo != 'undefined'){
cb(foo); // If foo is already define, I don't wait.
} else {
callback.push(cb);
}
}
这里的 async.function
只是一个占位符来象征一个异步调用.
Here async.function
is just a placeholder to symbolise an async call.
主要内容
var fooMod = require('./foo.js');
fooMod(function(foo){
//Here code using foo;
});
承诺方式
你也可以使用 Promise 来解决这个问题.该方法通过 Promise 的设计支持多次调用:
Promise way
You can also use Promise to solve that. This method support multiple call by the design of the Promise:
var foo, callback;
module.exports = new Promise(function(resolve, reject){
async.function(function(response) {
foo = "foobar"
resolve(foo);
});
});
这里的 async.function
只是一个占位符来象征一个异步调用.
Here async.function
is just a placeholder to symbolise an async call.
主要内容
var fooMod = require('./foo.js').then(function(foo){
//Here code using foo;
});
参见 Promise 文档
这篇关于异步 nodejs 模块导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!