1

I have the following code:

readInterface.on('line', async (line) => {
    const splitLine = line.split(',')

    const domain = splitLine[0]
    splitLine.shift()

    const query = `INSERT INTO domains (domain, ips) VALUES(?, ?);`;
    try {
      await client.execute(query, [ domain, splitLine ])
    } catch(e) {
        console.log(e)
    }
});

Currently this doesn't work. The code doesn't await before moving on to the next line. How can I change this to await the execute for each query keeping in mind the file is too big for node and cannot be read into memory?

0

1 Answer 1

4

If you use the asynchronous iterator of readline, you can, in serial, await each read line and await the database query Promise, so that each goes one-by-one without trying to do all of them all at once:

for await (const line of readInterface) {
    const [domain, ...splitLine] = line.split(',');
    const query = `INSERT INTO domains (domain, ips) VALUES(?, ?);`;
    try {
      await client.execute(query, [ domain, splitLine ])
    } catch(e) {
      console.log(e)
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Be wary of the for await () use with the readline module. It's full of bugs, particularly with error handling situations. I've filed a couple bugs on the module myself and the effort to fix them is very slow. As such, I do not use this construct in my code. In fact, I avoid the whole readline module as it's overloaded to try to do too many things (from tty console prompting to line processing in files) and that is probably partly responsible for the bugs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.