0

Consider these functions:

function waitASecA() {
  const promise = new Promise(resolve => setTimeout(resolve, 1000));

  return promise; // <--- don't await, just return
}


async function waitASecB() {
  const promise = new Promise(resolve => setTimeout(resolve, 1000));

  return await promise; // <--- first await, then return
}

As I understand they should be EXACTLY the same. Are there any scenarios when they'd differ in behavior?

Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
  • 1
    They are functionally equivalent. async/await is just syntactic sugar that makes the code a little easier to read, you do not have to use `.then` to utilize the results of a `Promise`. In the case of the above code though there is no difference. – Igor Dec 08 '22 at 17:58
  • 1
    Don't forget the third version `async function waitASecC() { …; return promise }` :-) – Bergi Dec 08 '22 at 18:00
  • 1
    You are still going to `await` the response when you call these anyways. Adding `await` inside is pointless. – Mr. Polywhirl Dec 08 '22 at 18:00
  • yes, that was exactly what I thought, thanks for confirming guys! – Alex Lomia Dec 08 '22 at 18:16

0 Answers0