我实在是想不通这个问题。我有一个包含文件的文件夹。我想创建一个包含这些文件信息的 json 。准确地说,是文件名、文件名的正则表达式版本以及我从 ajax 调用获得的附加数据。
各个部件工作完美。事实上,如果我同步运行我的代码,我就会得到我想要的结果。像这样的事情:
[
{
"filename" : "first file",
"regexname" : "first file",
"ajaxresult" : "first file"
},
{
"filename" : "second file",
"regexname" : "second file",
"ajaxresult" : "second file"
},
]
但我想异步执行,因为我使用的 XHR 模块在尝试“同步”执行所有操作时无法正常工作,而且它当然会阻塞,尤其是在处理大量文件时。 我的“同步”代码如下所示(稍微简化以使其更短)
fs.readdir(datapath, function(err, files) {
var data = new Array();
if(err) throw err;
files.forEach(function(file){
var filename = file
, regex= filename.replace(/regex/)/i, "")
, filepath = './public/config/dataconfig.js'
, ajaxResults = getFile(url)
, mapFiles = null
mapFiles = { filename:filename, regexname:regex, ajaxcall:ajaxResults}
data[data.length] = mapfiles;
}
var JSONdata= JSON.stringify(data, null, 4);
fs.writeFile(filepath, JSONdata, function(e) {
if (!e) {
console.log('Done')
}else{
console.log('Failed', e)
};
});
});
我尝试将 ajaxcall 包装在 promise 中,但我丢失了 promise 内的文件名和正则表达式名称。像这样:
fs.readdir(datapath, function(err, files) {
var data = new Array();
if(err) throw err;
files.forEach(function(file){
var filename = file
, regex= filename.replace(/regex/)/i, "")
, filepath = './public/config/dataconfig.js'
, mapFiles = null
// Make promise for async call with a url composed of the regex
var promise = getFile(url);
promise.then( function(result) {
mapFiles = { filename:filename, regexname:regex, ajaxcall:result}
data[data.length] = mapfiles;
var JSONdata= JSON.stringify(data, null, 4);
fs.writeFile(filepath, JSONdata, function(e) {
if (!e) {
console.log('Done')
}else{
console.log('Failed', e)
};
});
});
});
这给了我以下结果:
[
{
"filename" : "first file",
"regexname" : "first file",
"ajaxresult" : "first file"
},
{
"filename" : "first file",
"regexname" : "first file",
"ajaxresult" : "second file"
},
]
所以我最后的想法是使用类似于 Node 的异步模块之类的东西。在从函数返回回调后编写 json。但我也无法让它发挥作用。
提前致谢!
[编辑 - 我忘记提及的内容]
我忘记提及的一件事是 AjaxCall url 是使用正则表达式构建的。 所以像这样:
url = 'http://www.test.com/'+regex
所以这(据我所知)意味着我在调用 ajaxcall 之前执行正则表达式,但在调用之后我还需要正则表达式以写入 Json。我希望这是有道理的
[编辑 2 - 更多信息]
我的 XHR 调用如下所示(没有我错误的 promise 解决方案。在这种情况下,我会返回带有结果的 promise ):
// Get info
function getFile(url) {
xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onreadystatechange = handler
var ajaxResult = null
//Callback
function handler (){
// If status is ready
if (this.readyState == 4 && this.status == 200) {
ajaxResult = eval(this.responseText);
console.log('XHR OK')
};
};
xhr.send(null);
return ajaxResult[0];
};
最佳答案
基于 promise 的解决方案将正常工作,可能如下所示:
readdir(datapath).map(function (file) {
return getFile(url)(function (ajaxresult) {
return writeFile('./public/config/dataconfig.js', JSON.stringifiy({
filename: file,
regexname: file.replace(/regex/i, ""),
ajaxcall: ajaxResult
}, null, 4));
});
}).end(function () {
console.log("All done");
}, function (e) {
console.log("Failed", e);
});
在此示例中,我使用了 deferred Promise 实现,要使其正常工作,您需要确保所有异步函数都返回 Promise。典型的回调函数可以使用 deferred.promisify 轻松转换:
var promisify = require('deferred').promisify;
var readdir = promisify(fs.readdir);
var writeFile = promisify(fs.writeFile);
关于Javascript/NodeJs 不同的异步函数产生一个大的 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14883472/