10
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?

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
bbusdriver
  • 1,577
  • 3
  • 26
  • 58
  • 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 Jul 12 '19 at 20:14
  • if it's not useful at all, when should one use `(async () => { })();`? – bbusdriver Jul 12 '19 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 completed. – Patrick Roberts Jul 12 '19 at 20:39

1 Answers1

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();
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • Thanks. still not so sure when should one use `(async () => {})();` – bbusdriver Jul 12 '19 at 20:37
  • So if it resolves `this.doThis(a,b,c);` even before it resolves `this.test1();`, doesn't it mean that `this.doThis(a,b,c);` would error out? – bbusdriver Jul 12 '19 at 20:40
  • @bbusdriver I added a note about the "fire and forget" pattern. I think it fits what's going on in your example. – Mike Samuel Jul 12 '19 at 20:42
  • @bbusdriver Your question doesn't make any sense. It can never resolve `this.doThis(a,b,c);` before it resolves `this.test1();`. Please clarify what you're asking. – Patrick Roberts Jul 12 '19 at 20:43
  • 1
    @MikeSamuel in regards to the "fire and forget" pattern, isn't the outer function generally synchronous? There's no reason for it to return a promise that doesn't actually wait for anything. – Patrick Roberts Jul 12 '19 at 20:44
  • 1
    @bbusdriver. The promise returned by the whole may resolve before test1 resolves. The chain of awaits within the inner lambda still proceed in the order implied by the `await` statements. – Mike Samuel Jul 12 '19 at 20:45
  • @PatrickRoberts Yes, normally you'd do some work after the self-invoking lambda. I don't think it has to be synchronous though to qualify as an instance of the pattern. – Mike Samuel Jul 12 '19 at 20:48
  • @MikeSamuel Thanks. I have another [question](https://stackoverflow.com/questions/57014143/async-vs-callback) – bbusdriver Jul 12 '19 at 21:21