javascript - 订单丢失(Javascript 和 Node.js)
问题描述
我正在尝试做一个自动视频创建系统并且有一个主要问题。问题在这里消失了。程序正常运行,但输出的打印顺序不正确。
let fullPath = "script.txt"
fs.readFile(fullPath, 'UTF-8', async (err, data) => {
if (err) {
console.error("READSCRIPTERROR: " + err);
return
}
let lines = data.split(/\r?\n/)
lines.forEach(async line => {
let res = await findimage(line)
let filename = Math.floor(Math.random() * 10000).toString();
txtomp3.getMp3(replaceall("**", "", line)).then(async function (binaryStream) {
let file = fs.createWriteStream("./voice/" + filename + ".mp3");
file.write(binaryStream);
file.end();
voices.push("./voice/" + filename + ".mp3")
getAudioDurationInSeconds("./voice/" + filename + ".mp3").then((duration) => {
let dur = duration.toString().split(".")[0]
console.log("Line" + line);
mainImage(dur, line, filename, res)
});
})
})
})
其他一切都在工作,但我只是没有让它工作。
在我的文件中是
你好1 你好2 你好3
但在那之后它就像随机
系统正在打印Test1 Hello Test2
虽然它应该以正确的顺序打印,即Hello Test1 Test2
解决方案
发生这种情况是因为 aforEach
和.then()
call 都不会阻止下一个语句/迭代同步执行。
一个好的经验法则是:
- 不要将
async
函数作为forEach
回调传递;改用for
循环。 - 不要混合使用
async
和.then()
。选择一种模式并坚持下去
以下是简化异步的方法:
let fullPath = "script.txt"
fs.readFile(fullPath, 'UTF-8', async (err, data) => {
if (err) {
console.error("READSCRIPTERROR: " + err);
return;
}
let lines = data.split(/\r?\n/);
for (let line of lines) {
let res = await findimage(line);
let filename = Math.floor(Math.random() * 10000).toString();
let binaryStream = await txtomp3.getMp3(replaceall("**", "", line));
let file = fs.createWriteStream("./voice/" + filename + ".mp3");
file.write(binaryStream);
file.end();
voices.push("./voice/" + filename + ".mp3");
let duration = await getAudioDurationInSeconds("./voice/" + filename + ".mp3");
let dur = duration.toString().split(".")[0];
console.log("Line" + line);
mainImage(dur, line, filename, res);
}
});
这假设您未提供的功能按预期工作。例如,如果mainImage
是一个具有异步行为并返回承诺的函数,那么您也应该使用await
它。
推荐阅读
- c# - 带有 DBQuery 的 Entity Framework Core 2 - 获取未设置为对象实例的对象引用
- xcode - 如何在 xcode 11.2 中链接 ftdi d2xx dylib?
- angular - 在更新新数据标记之前删除标记
- influxdb - 涌入数据库 | 获取最后一点和第一点之间的经过时间
- google-cloud-platform - 当使用相同的子网 IP 创建两个 GCP VPN 时,会导致故障排除
- rstudio - 如何将 Azure Databricks 中的 RStudio 连接到 Delta Lake
- windows - 批处理命令以列出文件夹中的文件并添加文本
- angular - 仅在组件中注入 ngrx 功能状态
- python - 即使使用管理员用户也无法使 Python WMI 工作
- macos - mac 的 install4j 8 和公证不起作用