【发布时间】:2019-07-13 04:42:23
【问题描述】:
async function test() {
(async () => {
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
})();
}
将方法 (test1,test2,test3) 放入async () => {})() 是什么意思?
我发现它比
async function test() {
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
}
使用它有什么缺点吗?
【问题讨论】:
-
它似乎更快,因为返回的承诺实际上并没有等待您的任何顺序逻辑。您不妨将其更改为同步函数,因为在第一种情况下返回的承诺基本上是
return Promise.resolve();,这根本没有用。 -
如果一点用都没有,什么时候应该使用
(async () => { })();? -
在顶层,当您想要访问
async/await语法时。如果该表达式存在于另一个函数中,则意味着该函数的调用者将无法知道异步调用何时完成。