我已经阅读了async/await,在阅读了几篇文章之后,我决定自己测试一下。然而,我似乎不明白为什么这行不通:
async function main() {
var value = await Promise.resolve('Hey there');
console.log('inside: ' + value);
return value;
}
var text = main();
console.log('outside: ' + text);
控制台输出以下内容(节点v8.6.0):
> outside: [object Promise]
> inside:嘿,大家好
为什么函数内部的日志消息之后执行?我认为创建async/await的原因是为了使用异步任务执行同步执行。
是否有一种方法可以使用函数内部返回的值,而不使用main()后的.then() ?
其他解决方案缺乏POSIX兼容性的一些重要细节:
你需要…
成功时报告0退出状态,失败时报告非0退出状态。
向标准输出流发出错误。
#!/usr/bin/env node
async function main() {
// ... await stuff ...
}
// POSIX compliant apps should report an exit status
main()
.then(() => {
process.exit(0);
})
.catch(err => {
console.error(err); // Writes to stderr
process.exit(1);
});
如果使用command等命令行解析器,则可能不需要main()。
例子:
#!/usr/bin/env node
import commander from 'commander'
const program = new commander.Command();
program
.version("0.0.1")
.command("some-cmd")
.arguments("<my-arg1>")
.action(async (arg1: string) => {
// run some async action
});
program.parseAsync(process.argv)
.then(() => {
process.exit(0)
})
.catch(err => {
console.error(err.message || err);
if (err.stack) console.error(err.stack);
process.exit(1);
});
如果您的唯一目标是为了测试目的控制混合了其他代码的异步代码的执行顺序,那么您可以将整个顶级代码包装在定义为异步函数的立即调用函数表达式(IIFE)中。在问题的示例中,在调用main()之前添加await。
当您的代码不在异步函数中或不在模块的顶层主体中时,可以使用此模式。换句话说,如果你只是在一个js文件中测试一堆代码,并使用Live Server、RunJs或任何其他类型的JavaScript工具来观察控制台窗口,将所有代码包装在定义为异步的IIFE中,当你想在执行下一行之前等待异步代码完成时,使用await关键字。
let topLevelIIFE = (async () => {
异步函数main() {
var value = await承诺。解决('嘿');
Console.log ('inside: ' + value);
返回值;
}
Var文本=等待main();
Console.log ('outside: ' + text);
}) ()
在Chrome DevTools或其他行为类似的浏览器REPL工具的REPL中运行IIFE主体中指定的代码时,您不需要使用此模式。