main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
async function doubleAndAdd1(a, b) {
  a = await doubleAfter1Sec(a);
  b = await doubleAfter1Sec(b);
  return a + b;
}

async function doubleAndAdd2(a, b) {
  a = doubleAfter1Sec(a);
  b = doubleAfter1Sec(b);
  return await a + await b;
}

function doubleAfter1Sec(param) {
  return new Promise(resolve => {
    setTimeout(() => resolve(param * 2), 1000);
  });
}

console.time("timer1");
doubleAndAdd1(1, 2).then(result => {
  console.log(result);
  console.timeEnd("timer1")
});

console.time("timer2");
doubleAndAdd2(1, 2).then(result => {
  console.log(result);
  console.timeEnd("timer2")
});
Native Browser JavaScript