英文:
How to use async/await while reading a file using readline?
问题
The line console.log('data', res)
在初始化res
之前执行,所以输出是reading file data undefined reading complete
。要等待res
初始化后再执行,你可以在reader.on("close", async () => {...})
的回调函数中,将console.log('data', res)
移到该回调函数内部,确保在文件读取完成后才执行这个操作。修改后的代码如下:
const fs = require('fs');
const rl = require("readline");
async function readTwoColumnFile() {
console.log('reading file');
// (C) READ LINE-BY-LINE INTO ARRAY
const reader = rl.createInterface({
input: fs.createReadStream("index.js")
});
reader.on("line", (row) => {
// some code
});
// (D) DONE - FULL ARRAY
reader.on("close", async () => {
// some code
console.log('reading complete');
const res = 'Hello World!';
console.log('data', res);
});
}
async function run(){
await readTwoColumnFile();
}
run();
这样,你可以确保res
在文件读取完成后才会被初始化和打印出来。
英文:
This code is not giving output as I want.
const fs = require('fs');
const rl = require("readline");
async function readTwoColumnFile() {
console.log('reading file');
// (C) READ LINE-BY-LINE INTO ARRAY
const reader = rl.createInterface({
input: fs.createReadStream("index.js")
});
reader.on("line", (row) => {
//some code
});
// (D) DONE - FULL ARRAY
reader.on("close", async () => {
// some code
console.log('reading complete')
res = 'Hello World!'
return res
});
}
async function run(){
const res = await readTwoColumnFile()
console.log('data' , res)
}
run()
Here the line console.log('data', res)
is executing without res
being initialized so when I run this code my output is coming
reading file
data undefined
reading complete
Instead of
reading file
reading complete
data Hello World!
So how can I wait for res
to get executed after initilazation?
答案1
得分: 2
在readTwoColumnFile
函数中,你需要返回一个新的Promise实例。
const fs = require('fs');
const rl = require("readline");
function readTwoColumnFile() {
return new Promise((resolve, reject) => {
console.log('reading file');
// (C) READ LINE-BY-LINE INTO ARRAY
const reader = rl.createInterface({
input: fs.createReadStream("index.js")
});
reader.on("line", (row) => {
//some code
});
reader.on('error', reject);
// (D) DONE - FULL ARRAY
reader.on("close", async () => {
// some code
console.log('reading complete')
res = 'Hello World!'
resolve(res);
});
});
}
async function run(){
const res = await readTwoColumnFile()
console.log('data' , res)
}
run()
英文:
You need to return a new Promise instance in readTwoColumnFile
.
const fs = require('fs');
const rl = require("readline");
function readTwoColumnFile() {
return new Promise((resolve, reject) => {
console.log('reading file');
// (C) READ LINE-BY-LINE INTO ARRAY
const reader = rl.createInterface({
input: fs.createReadStream("index.js")
});
reader.on("line", (row) => {
//some code
});
reader.on('error', reject);
// (D) DONE - FULL ARRAY
reader.on("close", async () => {
// some code
console.log('reading complete')
res = 'Hello World!'
resolve(res);
});
});
}
async function run(){
const res = await readTwoColumnFile()
console.log('data' , res)
}
run()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论