我们如何在需要时保证 Javascript 中的同步行为?

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

我是 Javascript 编程的新手,这是一个有点人为的例子,但它是我遇到的许多问题的核心,我需要在 javascript 中对某些处理进行同步排序.

假设我有两个文本文件 Sample1.text 其中包含

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

和 Sample2.txt 其中包含

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

我需要处理它们,以便 Sample1.txt 总是在 Sample2.txt

之前完成处理

这是我的代码:

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

并且输出:

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

如何保证订单?这样我总能保证顺序,这样我总能得到:

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

我试图了解我们如何在需要时保证同步行为。 是否可以使用 for await 构造将函数 processLineByLine_1_7 包装在一个 promise 中,或者有其他方法吗?

如果我试试这个:

async function main() {

  await processLineByLine_1_7();
  await processLineByLine_8_14();
}

主要();

我看到以下输出:

> 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

您需要在 processLineByLine_1_7 & processLineByLine_8_14 前面附加 await

await processLineByLine_1_7();
await processLineByLine_8_14();

我认为问题在于您的 readline interfaces 已经开始处理,因此顺序混乱了。尝试仅在实际需要时创建它们,例如:

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

注意:显然这可以重构为“更干”