您好,我试图了解以下代码中发生的情况:
import fs from "fs";
import util from "util";
import split2 from "split2";
async function main() {
const files = await fs.promises.readdir("..");
for (const f of files) {
const s = fs.createReadStream("../" + f);
const ss = s.pipe(split2());
s.on("error", (err) => {
console.log(err);
});
for await (const l of ss) {
}
console.log(f);
}
console.log("Returning");
return "Done";
}
main();
它基本上读取目录中的每个文件并迭代每个文件的行(通过使用 forawait 语法将可读流通过管道输送到 split2 库中。问题是,当文件确实是目录时,createReadStream 失败(随后什么也没有发生)奇怪的是,似乎一个错误导致主函数默默退出并在错误之前仅显示几个文件名。
.gitignore
app.js
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
如果我只是评论await for 循环,我会得到以下输出。
.gitignore
app.js
business
components
config
env.skeletron
node_modules
package-lock.json
Returning
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
}
这确实是完整的文件列表,并且各个错误不会停止外循环。我在 Ubuntu 机器上使用节点 v14.0.0 运行它。知道发生了什么事吗?我真的迷路了:-D
Helenr
相关分类