【发布时间】:2021-05-13 23:51:29
【问题描述】:
我是 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();
}
main();
我看到以下输出:
> 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
【问题讨论】:
-
await processLineByLine_1_7();await processLineByLine_8_14();??? -
感谢 Keith,但 await 仅在异步函数和模块的顶层主体中有效。我也相信 processLineByLine_1_7() 需要返回一个承诺。
-
是的,所以你把它放在一个
async函数中并运行它..processLineByLine_1_7()返回一个 Promise。 -
async function main() { //if (isPromise(processLineByLine_1_7)) console.log("Returns a promise");等待 processLineByLine_1_7();等待 processLineByLine_8_14(); } 主要的();输出 > 节点。 ` 阅读此行:第 8 行 阅读此行:第 9 行 阅读此行:第 10 行 阅读此行:第 11 行 阅读此行:第 12 行 阅读此行:第 13 行 阅读此行:第 14 行`
-
请注意,您在
1_7中使用rlp2,在8_14中使用rlp1。我想这是一个错字?!
标签: javascript node.js asynchronous es6-promise synchronous