Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for promises? #1

Open
timkay opened this issue Apr 4, 2019 · 0 comments
Open

Add support for promises? #1

timkay opened this issue Apr 4, 2019 · 0 comments

Comments

@timkay
Copy link

timkay commented Apr 4, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant