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.