-1
async function test() {
  for (var i = 0; i < balance; i++) {      
    (async () => {            
      var a = await this.test1(i);
      var b = await this.test2(a);
      var c = await this.test3(b);  
      this.doThis(a,b,c);                              
    })();
  }
}

So I've concluded that (async () => {})(); is quite similar to a good old callbacks

function test() {
  for (var i = 0; i < balance; i++) {      
    test1().call().then((a) => {           
      test2().call(a).then((b) => {
        test3().call(b).then((c) => {
           this.doThis(a,b,c);   
         });
      });
    });
  }
}

Is this right? performance-wise I find they are about the same.

  1. Do they have any differences?
  2. What pattern should I follow?

I want to go with the first one async/await, but if I take out (async () => {})(); I find it slower to render stuff in UI compare to the second one.

bbusdriver
  • 1,577
  • 3
  • 26
  • 58
  • 1
    If you add some loops / ifs, thats easy to do with the first pattern but it gets pain with the second. Some say that `async/awaut` is *just* syntactic sugar, while that applies to some cases (e.g. yours) it doesn't really apply to more complicated ones. – Jonas Wilms Jul 12 '19 at 22:06

1 Answers1

1

Do they have any differences?

You should return from the then callbacks so that errors do propagate properly. Apart from that, they're basically equal.

What pattern should I follow?

Both of them are fire-and-forget calls, on which you should not forget to handle errors!

I want to go with the async/await, but if I take out (async () => {})(); I find it slower

Sure, because then you are awaiting in the loop so it's becoming sequential.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375