TransWikia.com

How we can guarantee synchronous behaviour in Javascript when we need it?

Stack Overflow Asked by Johnos on February 12, 2021

I am new to Javascript programming and this is a somewhat contrived example, but it is at the heart of a number of problems I have where I need to synchronously sequence some processing in javascript.

Say I have two text files
Sample1.text which contains

line number 1
line number 2
line number 3
line number 4
line number 5
line number 6
line number 7

And Sample2.txt which contains

line number 8
line number 9
line number 10
line number 11
line number 12
line number 13
line number 14

I need to process them so that Sample1.txt always completes processing before Sample2.txt

Here is my code:

const fs = require("fs");
const { mainModule } = require("process");
// const readline = require("readline");

const readline = require("readline-promise").default;

const rlp2 = readline.createInterface({
  terminal: false,
  input: fs.createReadStream("sample2.txt"),
});

const rlp1 = readline.createInterface({
  terminal: false,
  input: fs.createReadStream("sample1.txt"),
});

// No top-level await in JS yet, so we put it in an async function
async function processLineByLine_1_7() {
  for await (const line of rlp2) console.log(`Read this line: ${line}`);
}

// No top-level await in JS yet, so we put it in an async function
async function processLineByLine_8_14() {
  for await (const line of rlp1) {
    console.log(`Read this line: ${line}`);
  }
}

processLineByLine_1_7();
processLineByLine_8_14();

And the output:

Read this line: line number 8
Read this line: line number 9
Read this line: line number 10
Read this line: line number 11
Read this line: line number 12
Read this line: line number 13
Read this line: line number 1
Read this line: line number 2
Read this line: line number 3
Read this line: line number 4
Read this line: line number 5
Read this line: line number 6
Read this line: line number 7
Read this line: line number 14

How do I guarantee the order? So that I always guarantee the order so that I always get:

Read this line: line number 1
Read this line: line number 2
Read this line: line number 3
Read this line: line number 4
Read this line: line number 5
Read this line: line number 6
Read this line: line number 7
Read this line: line number 8
Read this line: line number 9
Read this line: line number 10
Read this line: line number 11
Read this line: line number 12
Read this line: line number 13
Read this line: line number 14

I trying to understand how we can guarantee synchronous behaviour when we need it.
Is it possible to wrap the functions processLineByLine_1_7 in a promise with for await constructs or is there some other way?

If I try this:

async function main() {

  await processLineByLine_1_7();
  await processLineByLine_8_14();
}

main();

I see the following output:

> node .

Read this line: line number 8
Read this line: line number 9
Read this line: line number 10
Read this line: line number 11
Read this line: line number 12
Read this line: line number 13
Read this line: line number 14

2 Answers

I think the problem is that your readline interfaces already start processing, as such the order is mixed up. Try only creating them when actually needed, e.g.:

const fs = require("fs");
const readline = require("readline-promise").default;

async function processLineByLine_1_7() {
  const rl = readline.createInterface({
    input: fs.createReadStream("sample1.txt"),
  });

  for await (const line of rl) {
    console.log(`Read this line: ${line}`);
  }
}

async function processLineByLine_8_14() {
  const rl = readline.createInterface({
    input: fs.createReadStream("sample2.txt"),
  });

  for await (const line of rl) {
    console.log(`Read this line: ${line}`);
  }
}

(async () => {
  await processLineByLine_1_7();
  await processLineByLine_8_14();
})();

Note: Obviously this could be refactored to be "more dry".

Answered by Yoshi on February 12, 2021

You need to attach await in front of processLineByLine_1_7 & processLineByLine_8_14.

await processLineByLine_1_7();
await processLineByLine_8_14();

Answered by Prime on February 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP