最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (async () => { })(); what is this? - Stack Overflow

programmeradmin3浏览0评论
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);                              
  })();
}

What does it mean to put methods (test1,test2,test3) inside async () => {})()? I find it faster than

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); 
}

Any downside of using it?

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);                              
  })();
}

What does it mean to put methods (test1,test2,test3) inside async () => {})()? I find it faster than

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); 
}

Any downside of using it?

Share Improve this question edited Jul 12, 2019 at 20:20 Andrew Li 58k14 gold badges134 silver badges148 bronze badges asked Jul 12, 2019 at 20:04 bbusdriverbbusdriver 1,6274 gold badges29 silver badges67 bronze badges 3
  • It just seems faster because the returned promise doesn't doesn't actually wait for any of your sequential logic. You might as well change it to a synchronous function since the returned promise is basically a return Promise.resolve(); in the first case, which isn't useful at all. – Patrick Roberts Commented Jul 12, 2019 at 20:14
  • if it's not useful at all, when should one use (async () => { })();? – bbusdriver Commented Jul 12, 2019 at 20:36
  • At the top level, when you want access to async / await syntax. If that expression exists within another function, it means the caller of that function will not be able to know when the asynchronous calls have pleted. – Patrick Roberts Commented Jul 12, 2019 at 20:39
Add a ment  | 

1 Answer 1

Reset to default 9

Both return a promise but they return different promises.

The first will return a promise that may resolve before this.test1()'s result resolves.

The second returns a promise that only resolves after the final call to this.doThis(a,b,c);.

This has been called the "fire and forget pattern":

Often in application development you want a process to call another thread and continue the process flow, without waiting for a response from the called thread. This pattern is called the “fire and forget” pattern.

You can see this in

function logEventually(str) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(str);
      resolve(null);
    }, 0);
  });
}

async function a() {
  await logEventually('in a 1');
  await logEventually('in a 2');
  await logEventually('in a 3');
  return await logEventually('end of a');
}

async function b() {
  (async () => {
    await logEventually('in b 1');
    await logEventually('in b 2');
    await logEventually('in b 3');
  })();
  return await logEventually('end of b');
}

a();
b();

发布评论

评论列表(0)

  1. 暂无评论