-1

And also, if Async function return a promise, it doesnt return the same promise, but that promise is somehow changed so its not equals itself. And is there a difference if I would return a promise synchronously without any awaits, or with them?

let promise = new Promise(resolve => resolve());

async function test() {
  
  return promise;

}

console.log(promise === test()); // false
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • You're trying to compare two different things so the result will always be `false`. – Andy Jun 09 '22 at 18:49
  • 1
    The whole point of an `async` function is that whatever you return inside the function gets implicitly wrapped in a promise. So your code is basically wrapping a promise in another promise, which is why they don't have the same identity. You shouldn't mix `async` functions with explicit promises. The point of `async` is that you **don't** need to return a promise explicitly. – Lennholm Jun 09 '22 at 18:51
  • But what exactly happens when I return promise from async function. Apparently the result isnt that promise, but than what. What if returned promise has no value yet. No answers anywhere, on mdn, on stackoverflow, nothing. – flasher1101 Jun 09 '22 at 18:54
  • It returns a promise that resolves to the underyling promise. From there, promise unwrapping semantics make it seamless. The answers you want are in the async/await language docs & the promise spec. if the promise "has no value yet" then it's said to be in a "pending" state, and when it resolves, you'll get the data. That's the whole point of promises. – CollinD Jun 09 '22 at 18:55
  • async function without an await expression will run synchronously. – Nexo Jun 09 '22 at 18:56
  • It'll still return a promise, regardless of the body running synchronously or not. – CollinD Jun 09 '22 at 18:56
  • What does it means wrapping promise in another promise. Apparently the returned promise doesnt become a resolved value of async. But what happens then – flasher1101 Jun 09 '22 at 18:58
  • 2
    @Lennholm "You shouldn't mix async functions with explicit promises." is misleading. It's fine and most common to return an existing promise. Even if you have to create an explicit promise, I don't know why you're suggesting the function should not be async, – Ruan Mendes Jun 09 '22 at 18:58
  • 1
    "*But what exactly happens when I return promise from async function. Apparently the result isnt that promise, but than what. What if returned promise has no value yet. No answers anywhere, on mdn, on stackoverflow, nothing.*" there have been *plenty* of answers all over the place. [Here is an explanation that async functions wrap the result in a promise](https://stackoverflow.com/a/62196933) and then [here is an answer on SO citing MDN](https://stackoverflow.com/a/46490779) that explains what happens if you wrap a promise in a promise. – VLAZ Jun 09 '22 at 19:08

1 Answers1

1

The async keyword always makes a function create and return a new promise which will only get resolved after all the work (asynchronous and synchronous) of that function is complete.

In other languages like C# you would have to return await promise, or the calling code would have to await await test(), since return promise would cause the async function to return a promise with another promise inside.

In JavaScript, when you return a promise from within an async function, the promise that actually gets returned from your function knows to effectively await that other promise and resolve with the value (if any) that it returns. This behavior is merely a convenience, because there are very, very few use cases for actually returning nested promises, while it's highly likely for someone to make the mistake of forgetting to await a promise before returning it. But the async function is still returning a new promise, not the one provided by the return statement.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Wow!, Thx. But there isnt such info in spec or anywhere. Just hilarious how such basic things are absent in all guides and even the spec. Thx again – flasher1101 Jun 09 '22 at 19:02
  • @flasher1101: That behavior is called out pretty explicitly [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#sect3). And while I'm not intimately familiar with the 1100+ pages of the [spec](https://262.ecma-international.org/12.0/), it's pretty safe to assume it's in there too. – StriplingWarrior Jun 09 '22 at 19:13
  • @flasher1101 how is that not part of the spec or anywhere?! It was defined in [Promises/A+](https://promisesaplus.com/). It's also in the [ECMAScript spec](https://262.ecma-international.org/6.0/#sec-promise-objects). Also [in MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#return_value). – VLAZ Jun 09 '22 at 19:13