Answer the question
In order to leave comments, you need to log in
What is the best way to mark the last iteration of the loop?
There is a function that takes the path to the file and callback as input,
the function itself asynchronously reads the file line by line and calls the callback with the current line and the value of the end of reading.
How can we improve the transmission of information about the last line without calling callback with an empty string?
a simple example to illustrate:
const fs = require('fs');
const readline = require('readline');
async function read(filepath, callback) {
const fileStream = fs.createReadStream(filepath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
for await (const line of rl) {
await callback(line, false);
}
// ?????????????????
await callback(null, true);
}
read('./hello.json', (line, done) => {
console.log(line, done);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question