As the title states, I was wondering if it is possible to use promisify (https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original) for readline in node.js? I was only able to do it like this:
let data = [];
const parse = () => {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: fs.createReadStream(path)
});
rl.on('line', (line) => {
data.push(line);
});
rl.on('close', () => {
resolve(data);
});
});
};
promisify.customother types of callbacks that does not follow the pattern. For streams and event emitters you need to implement your own logic, it does share a common interface (with onclose, onfinish) but the usecases are very different.datain a higher scope than your promise, it will accumulate data for eachparseuse, if you useparsepromise 2 times, the second use will have the first values appended and then the second values also. A better approach is to set thelet datain the promise in this way for each use you will store only the new data.