tz4life

Joined on Jul 13th 2017
Karma

27

  • As you can see in the simple example of async function on MDN. You can achieve what Promise.all does with async/await aswell.

    function resolveAfter2Seconds(x) {
      return new Promise(resolve => {
        setTimeout(() => {
          resolve(x);
        }, 2000);
      });
    }
    
    async function add1(x) {
      var a = resolveAfter2Seconds(20);
      var b = resolveAfter2Seconds(30);
      return x + await a + await b;
    }
    
    add1(10).then(v => {
      console.log(v);  // prints 60 after 2 seconds.
    });
    
    async function add2(x) {
      var a = await resolveAfter2Seconds(20);
      var b = await resolveAfter2Seconds(30);
      return x + a + b;
    }
    
    add2(10).then(v => {
      console.log(v);  // prints 60 after 4 seconds.
    });