如何在使用readline读取文件时使用async/await?

huangapple go评论62阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年1月4日 18:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75004406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定