1

I'm wondering if async/await behave the same in the following two 'super-basic' examples:

async function blah1() {
  return await foo.bar().then('Done');
}

as this

async function blah2() {
  return blah3(foo.bar());
}

async function blah3(fn) {
  return await fn.then('Done');
}

or is there some important difference?

  • Yes I know 'return await' is redundant but I left it in for this example :)
4
  • I'd start with then expecting a function, or, "If it is not a function, it is internally replaced with an "Identity" function". Then you wrote "done" once, then "Done". Last but least, there are semantical differences, depending on how you use these functions, but understanding the signatures comes first. Commented Feb 19, 2020 at 18:17
  • Yes, these are super basic examples. I don't care what happens inside the then for this example, so I just wrote something quick to demonstrate a then may take place Commented Feb 19, 2020 at 18:24
  • I don't think there is a functional difference. Have you observed one? Commented Feb 19, 2020 at 18:26
  • Not offhand. Just trying to wrap my head around that scenario and making sure there's not an obvious reason not to do the second option Commented Feb 19, 2020 at 19:12

1 Answer 1

2
async function blah1() {
  return await foo.bar().then('Done');
}

blah1()

Calls foo.bar, which returns a promise, to which a then is added.

The resultant promise is returned.

async function blah2() {
  return blah3(foo.bar());
}

async function blah3(fn) {
  return await fn.then('Done');
}

blah2()

Calls foo.bar, which returns a promise, which is passed to blah3, which adds a then.

The resultant promise is returned from blah3 to blah2 and thence to the caller.

I'd say no meaningful behavioral difference.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.