简单的需求如下:
download/目录下有一个文件: query.list,这个文件记录了爬取的文件列表【格式如下】,我想读取列表信息、以及文件内容,并渲染页面。
++++++++++++++++++++++++++++++++++++
查询生成 1 个文档, 有<a href="docs/" class="docs-link">1个文书</a>可以查看
{"title": "上海XX贸易有限公司与XXX劳动合同纠纷", "court": "上海市第二中级人民法院", "caseno": "(2015)沪二中民三(民)终字第XXX号", "date": "2015-11-25", "filename": "./download/1.txt", "docid": "1"}
++++++++++++++++++++++++++++++++++++
var fs = require('fs');
async function read_file(filename) {
return new Promise(function(resolve, reject) {
fs.readFile(filename, 'utf8', function(error, data) {
if (error) return reject(error);
resolve(data);
});
});
};
async function htmlize(fulltext) {
return new Promise(function(resolve, reject) {
... // 将文本文件html化,便于显示
console.log(inner_html);
resolve(inner_html);
});
};
var asyn_func = async function() {
data = await read_file('./download/query.list');
lines = data.split("\n");
file_list = []
for (var i = 1; i < lines.length; ++i) {
if (lines[i] == '') {
continue;
}
obj = JSON.parse(lines[i]);
console.log(obj);
file_list.push(obj['filename']);
}
return file_list.map(async (filename) => {
var data = await read_file(filename);
var html = await htmlize(data); /// !!!! 这里会卡住
return html;
});
}
asyn_func().then(v => Promise.all(v)).then(function(result) {
console.log(result);
}).catch(function(err) {
console.log(err);
});
--
FROM 58.100.110.*