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

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

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)移到该回调函数内部,确保在文件读取完成后才执行这个操作。修改后的代码如下:

  1. const fs = require('fs');
  2. const rl = require("readline");
  3. async function readTwoColumnFile() {
  4. console.log('reading file');
  5. // (C) READ LINE-BY-LINE INTO ARRAY
  6. const reader = rl.createInterface({
  7. input: fs.createReadStream("index.js")
  8. });
  9. reader.on("line", (row) => {
  10. // some code
  11. });
  12. // (D) DONE - FULL ARRAY
  13. reader.on("close", async () => {
  14. // some code
  15. console.log('reading complete');
  16. const res = 'Hello World!';
  17. console.log('data', res);
  18. });
  19. }
  20. async function run(){
  21. await readTwoColumnFile();
  22. }
  23. run();

这样,你可以确保res在文件读取完成后才会被初始化和打印出来。

英文:

This code is not giving output as I want.

  1. const fs = require('fs');
  2. const rl = require("readline");
  3. async function readTwoColumnFile() {
  4. console.log('reading file');
  5. // (C) READ LINE-BY-LINE INTO ARRAY
  6. const reader = rl.createInterface({
  7. input: fs.createReadStream("index.js")
  8. });
  9. reader.on("line", (row) => {
  10. //some code
  11. });
  12. // (D) DONE - FULL ARRAY
  13. reader.on("close", async () => {
  14. // some code
  15. console.log('reading complete')
  16. res = 'Hello World!'
  17. return res
  18. });
  19. }
  20. async function run(){
  21. const res = await readTwoColumnFile()
  22. console.log('data' , res)
  23. }
  24. run()

Here the line console.log('data', res) is executing without res being initialized so when I run this code my output is coming

  1. reading file
  2. data undefined
  3. reading complete

Instead of

  1. reading file
  2. reading complete
  3. data Hello World!

So how can I wait for res to get executed after initilazation?

答案1

得分: 2

readTwoColumnFile函数中,你需要返回一个新的Promise实例。

  1. const fs = require('fs');
  2. const rl = require("readline");
  3. function readTwoColumnFile() {
  4. return new Promise((resolve, reject) => {
  5. console.log('reading file');
  6. // (C) READ LINE-BY-LINE INTO ARRAY
  7. const reader = rl.createInterface({
  8. input: fs.createReadStream("index.js")
  9. });
  10. reader.on("line", (row) => {
  11. //some code
  12. });
  13. reader.on('error', reject);
  14. // (D) DONE - FULL ARRAY
  15. reader.on("close", async () => {
  16. // some code
  17. console.log('reading complete')
  18. res = 'Hello World!'
  19. resolve(res);
  20. });
  21. });
  22. }
  23. async function run(){
  24. const res = await readTwoColumnFile()
  25. console.log('data' , res)
  26. }
  27. run()
英文:

You need to return a new Promise instance in readTwoColumnFile.

  1. const fs = require('fs');
  2. const rl = require("readline");
  3. function readTwoColumnFile() {
  4. return new Promise((resolve, reject) => {
  5. console.log('reading file');
  6. // (C) READ LINE-BY-LINE INTO ARRAY
  7. const reader = rl.createInterface({
  8. input: fs.createReadStream("index.js")
  9. });
  10. reader.on("line", (row) => {
  11. //some code
  12. });
  13. reader.on('error', reject);
  14. // (D) DONE - FULL ARRAY
  15. reader.on("close", async () => {
  16. // some code
  17. console.log('reading complete')
  18. res = 'Hello World!'
  19. resolve(res);
  20. });
  21. });
  22. }
  23. async function run(){
  24. const res = await readTwoColumnFile()
  25. console.log('data' , res)
  26. }
  27. 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:

确定