簡體   English   中英

我們如何在需要時保證 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();

和 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

如何保證訂單? 這樣我總是保證訂單,以便我總是得到:

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

主要的();

我看到以下 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

您需要在processLineByLine_1_7processLineByLine_8_14前面附加await

await processLineByLine_1_7();
await processLineByLine_8_14();

我認為問題在於您的readline 接口已經開始處理,因此順序混淆了。 僅在實際需要時嘗試創建它們,例如:

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

注意:顯然這可以重構為“更干”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM