nodejs逐行读取文件并在末尾获取其内容
nodejs read file line by line and get its content at the end
我想使用 nodejs 逐行读取文件,然后在完成后将 returned 结果作为 json 字符串获取。我试图这样做,但最后 console.log 打印未定义而不是列表。我得到了列表和承诺的结尾,但是如何 return 它到 main.js 中的调用函数?
我有我的 main.js 文件:
var fct = require('./romlist-parser');
console.log(fct.myfunction());
并且romlist-parser.js有以下内容:
var fs = require('fs');
var readline = require('readline');
exports.myfunction = function() {
var promise = new Promise(function(resolve,reject) {
var rd = readline.createInterface({
input: fs.createReadStream('Atari 2600.txt'),
console: false
});
var games = [];
rd.on('line', function(line) {
var arr = line.split(";");
games.push({name:arr[0], title:arr[1]});
});
rd.on('close', function() {
var json = JSON.stringify(games);
resolve(games);
});
});
promise.then((resolveResult) => {
console.log(resolveResult);
return resolveResult;
});
};
试试这个:
exports.myfunction = function() {
var promise = new Promise(function(resolve,reject) {
var games = [];
var rl = readline('./Atari 2600.txt'); // provide correct file path
rl.on('line', function (line, lineCount, byteCount) {
// console.log(lineCount, line, byteCount);
var arr = line.split(";");
games.push({name:arr[0], title:arr[1]});
})
.on('close', function() {
var json = JSON.stringify(games);
resolve(games); // resolve(json); may be??
})
.on('error', function (e) {
console.log("error", e);
// something went wrong
});
});
promise.then((resolveResult) => {
console.log(resolveResult);
return resolveResult;
});
};
P.S。此代码可以进一步改进,但为了简单起见,您的理解答案仅限于 post 中的 style/code posted。否则它可以根据样式改变样式。
我会将设置和累积结果的变量移动到封闭范围中,然后,最重要的是,return 来自创建它的函数的承诺。所以...
exports.myfunction = function(filename) {
let games = [];
let rd = readline.createInterface({
input: fs.createReadStream(filename),
console: false
});
return new Promise(function(resolve,reject) {
rd.on('line', function(line) {
var arr = line.split(";");
games.push({name:arr[0], title:arr[1]});
});
rd.on('close', function() {
var json = JSON.stringify(games);
resolve(games);
});
// on 'error' call reject(error)
});
};
// then elsewhere
const fct = require('./romlist-parser');
function someFunction() {
let promise = fct.myfunction('Atari 2600.txt');
return promise.then(result => {
console.log(result);
return resolveResult
});
}
我还需要处理大文件中行的好方法。
所以我做了这个。您可以轻松地逐行迭代文件、流、字符串、缓冲区
还支持反向模式!
请尝试一下,如果喜欢,请给我一个star!
https://github.com/sharpart555/nexline
const nl = nexline({
input: fs.openSync(path_to_file, 'r'), // input can be file, stream, string and buffer
});
console.log(await nl.next()); // 'foo'
console.log(await nl.next()); // 'bar'
console.log(await nl.next()); // 'baz'
console.log(await nl.next()); // null, If all data is read, then return null
我想使用 nodejs 逐行读取文件,然后在完成后将 returned 结果作为 json 字符串获取。我试图这样做,但最后 console.log 打印未定义而不是列表。我得到了列表和承诺的结尾,但是如何 return 它到 main.js 中的调用函数?
我有我的 main.js 文件:
var fct = require('./romlist-parser');
console.log(fct.myfunction());
并且romlist-parser.js有以下内容:
var fs = require('fs');
var readline = require('readline');
exports.myfunction = function() {
var promise = new Promise(function(resolve,reject) {
var rd = readline.createInterface({
input: fs.createReadStream('Atari 2600.txt'),
console: false
});
var games = [];
rd.on('line', function(line) {
var arr = line.split(";");
games.push({name:arr[0], title:arr[1]});
});
rd.on('close', function() {
var json = JSON.stringify(games);
resolve(games);
});
});
promise.then((resolveResult) => {
console.log(resolveResult);
return resolveResult;
});
};
试试这个:
exports.myfunction = function() {
var promise = new Promise(function(resolve,reject) {
var games = [];
var rl = readline('./Atari 2600.txt'); // provide correct file path
rl.on('line', function (line, lineCount, byteCount) {
// console.log(lineCount, line, byteCount);
var arr = line.split(";");
games.push({name:arr[0], title:arr[1]});
})
.on('close', function() {
var json = JSON.stringify(games);
resolve(games); // resolve(json); may be??
})
.on('error', function (e) {
console.log("error", e);
// something went wrong
});
});
promise.then((resolveResult) => {
console.log(resolveResult);
return resolveResult;
});
};
P.S。此代码可以进一步改进,但为了简单起见,您的理解答案仅限于 post 中的 style/code posted。否则它可以根据样式改变样式。
我会将设置和累积结果的变量移动到封闭范围中,然后,最重要的是,return 来自创建它的函数的承诺。所以...
exports.myfunction = function(filename) {
let games = [];
let rd = readline.createInterface({
input: fs.createReadStream(filename),
console: false
});
return new Promise(function(resolve,reject) {
rd.on('line', function(line) {
var arr = line.split(";");
games.push({name:arr[0], title:arr[1]});
});
rd.on('close', function() {
var json = JSON.stringify(games);
resolve(games);
});
// on 'error' call reject(error)
});
};
// then elsewhere
const fct = require('./romlist-parser');
function someFunction() {
let promise = fct.myfunction('Atari 2600.txt');
return promise.then(result => {
console.log(result);
return resolveResult
});
}
我还需要处理大文件中行的好方法。
所以我做了这个。您可以轻松地逐行迭代文件、流、字符串、缓冲区
还支持反向模式!
请尝试一下,如果喜欢,请给我一个star!
https://github.com/sharpart555/nexline
const nl = nexline({
input: fs.openSync(path_to_file, 'r'), // input can be file, stream, string and buffer
});
console.log(await nl.next()); // 'foo'
console.log(await nl.next()); // 'bar'
console.log(await nl.next()); // 'baz'
console.log(await nl.next()); // null, If all data is read, then return null