Skip to content

Add support for promises? #1

@timkay

Description

@timkay

It would be nice if LineReader supported promises. Here is a class that wraps the newLine method with a promise and an example of how useful it is.


const LineReader = require('node-line-reader').LineReader;

class Reader {
    constructor(file) {
        this.reader = new LineReader(file);
    }
    nextLine() {
        return new Promise((resolve, reject) => {
            this.reader.nextLine((err, line) => {
                if (err) return reject(err);
                resolve(line);
            });
        });
    }
}

async function main() {
    let reader = new Reader('nextline.js');
    for (let i = 1; i < 1000; i++) {
        let line = await reader.nextLine();
        if (line === null) break;
        console.log('', i, line);
    }
}

main();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions