[英]async/await concurrent function calls in javascript (Chain functions)
I think I'm probably misunderstanding how to use async/await, but I'm struggling to find a solution that doesn't dive into promises.我想我可能误解了如何使用 async/await,但我正在努力寻找一个不深入承诺的解决方案。
I have an if statement that contains two functions.我有一个包含两个函数的 if 语句。 I need functionTwo to run only after functionOne has completed.
我需要 functionTwo 仅在 functionOne 完成后运行。 adding async and await before them doesn't seem to work, but I don't think I'm a million miles away.
在它们之前添加 async 和 await 似乎不起作用,但我不认为我在一百万英里之外。
Any help would be great!任何帮助都会很棒! Thank you!
谢谢!
if (someBoolean) {
functionOne();
functionTwo();
}
if (someBoolean) {
async functionOne();
await functionTwo();
}
You use await
for both.您对两者都使用
await
。
if (someBoolean) {
await functionOne();
await functionTwo();
}
async
is used when you declare the functions声明函数时使用
async
async function functionOne() {... }
async/await uses Promises. async/await 使用 Promises。 So I think that's not the best way to go if you want to avoid Promises.
因此,如果您想避免 Promise,我认为这不是 go 的最佳方法。 Your best bet might be to include a callback in your first function.
你最好的选择可能是在你的第一个函数中包含一个回调。
function one(callback) {
/* do your stuff here */
if (typeof callback === 'function) {
callback()
}
}
function two() {/* do the second set of things here */}
if (someBoolean) {
one(two)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.