0

If I call an async function that doesn’t explicitly return anything from the await, will it return a resolved Promise? Or Void? Something else?

For example an IIFE like this, or this but it being called in the global scope by sync code:

(async myFunc() {
   const list = await someFunc();
   const anotherList = someOtherFunc(list);
   console.log(anotherList);
})();

I have it in my head that all async functions return a Promise.

8
  • 1
    what happens if you try it? Commented May 21, 2024 at 22:33
  • I’m on my phone 😛 and yes I wrote out the above function by hand lol Commented May 21, 2024 at 22:40
  • When you call a function it will return what you return in code. In your case, undefined
    – IT goldman
    Commented May 21, 2024 at 22:47
  • 3
    @ITgoldman async functions always return a promise.
    – pilchard
    Commented May 21, 2024 at 22:56
  • 1
    "I have it in my head that all async functions return a Promise." - yes it does. Why do you doubt it? What do you think is different for a "top-level call" or "IIFE"?
    – Bergi
    Commented May 22, 2024 at 0:25

2 Answers 2

1

If code inside the async IIFE is sync and no promise returned than it returns a resolved fullfilled promise. If it contains await or returns a promise it returns a pending promise:

let promise = (async () => {

})();

console.log(promise.toString());

promise = (async () => {
   const list = await 1;
})();

console.log(promise.toString());

enter image description here

5
  • Why is it PromiseState: “fulfilled” and <pending> in your second example? Is this weird because the await is acting like sync code by returning 1 immediately? Also I notice it doesn’t have a PromiseResult which I suppose I’d expect since the code in the Async function never sets the result Commented May 21, 2024 at 23:07
  • If it were to return list, the PromiseResult would be set to it, correct? Commented May 21, 2024 at 23:07
  • @MattStyles stackoverflow.com/questions/76939148/… Commented May 21, 2024 at 23:31
  • 2
    Thanks, my guess for why it looks like that then is that to the console it’s printed as in a pending state but when you expanded it, the Promise referenced is now fulfilled some ms after console logging it Commented May 21, 2024 at 23:37
  • @MattStyles exactly Commented May 22, 2024 at 0:00
0

Asynchronous functions are just a wrapper around the Promise object. Normally, one creates a promise and passes in a callback function. When ready, the programmer calls the then method, et cetera. When using the async keyword, it simply abstracts that system. Therefore, it will always return a promise, even if you return something else, because it isn't actually returning what you return.

console.log((async () => {
    const list = await someFunc();
    const anotherList = someOtherFunc(list);
    console.log(anotherList);
})().toString());

console.log((async () => {
    const list = await someFunc();
    const anotherList = someOtherFunc(list);
    console.log(anotherList);
    return "foo";
})().toString());

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.