-2
$ cat x.js
async function print() {
    console.log("abc");
}

print();
$ nodejs x.js
abc

How can it be?! print() returns a Promise object that isn't awaited, is it? If it is not awaited, then why is the console.log executed?

porton
  • 5,214
  • 11
  • 47
  • 95

2 Answers2

2

yeah, like choz said ,async functions returns a promise, even if you haven't defined a promise in your code.

i think this returned promise turns fulfilled after all promises in await statement get resolved.

i test it in the following code, it also returns promise, which only turns fulfilled after all promises go resolved(after 3000ms in this case):

async function print2() {
   await console.log("abc")
    await new Promise((res, rej) => {
        setTimeout(() => {res(33)},3000)
    })
    await new Promise((res, rej) => {
        setTimeout(() => {res(33)},50)
    })
}
Eason Yu
  • 319
  • 1
  • 5
  • "this returned promise turns fulfilled after all promises in await statement get resolved" - but there is no await statement! – porton Dec 24 '20 at 05:08
  • sorry, my statement was not clear, i mean in async functions,returned promise get resolved after all promises in the function get resolved(include those promise declared through await key-word, you know `await 3` works like `Promise.of(3)`).although i'm not very clear of the relation between the returned promise and other promises in above statement. – Eason Yu Dec 24 '20 at 06:13
0

You could say that an empty function itself, returns undefined (Which actually doesn't return anything). Take a look at the sample below;

function print() {}
var returnVal = print(); // undefined

You'll notice that returnVal is undefined.

Now, if we have something in the body of test() there, but you still don't pass any return value, then returnVal will still be undefined.

function print() {
    console.log('abc');
}
var returnVal = print(); // undefined

So, in order for a function to have a return value, one simply just needs to return it.

function print() {
    console.log('abc');
    return 1;
}
var returnVal = print(); // 1

When you conver it to async function. Quoting from MDN

  1. The body of an async function can be thought of as being split by zero or more await expressions. Top-level code, up to and including the first await expression (if there is one), is run synchronously. In this way, an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously.

  2. Code after each await expression can be thought of as existing in a .then callback. In this way a promise chain is progressively constructed with each reentrant step through the function. The return value forms the final link in the chain.

Now, based on the information above, here's what we know that refer to your question;

  1. Your print() does not return anything, which should be undefined
  2. The async function will complete asynchronously, meaning it will always return a Promise. Either pending, fulfilled or rejected

So, to say it in your question, here's what your function actually does;

async function print() {
    console.log("abc"); // Prints 'abc'
}

// Function above is equivalent to;

function printEquivalentInNonAsync() {
    console.log("abc"); // Prints 'abc'
}

var returnVal = print(); // `returnVal` is `undefined`

And, to answer your question

If it is not awaited, then why is the console.log executed?

Regardless the async function is awaited, it still executes! - Awaits just to ensure the line execution is halted until the Asynchronous function (Promise) has reached fulfilled or rejected. Note that, the first state of a Promise is pending.

choz
  • 17,242
  • 4
  • 53
  • 73
  • When is a non-awaited async function executed? What if I have `f()` without `await` where `f` is an async function in a sync function? – porton Dec 24 '20 at 09:03
  • 1
    If you use `await` to call your async function, it will return the resolved value instead of a promise, which is `undefined` in your case. If you don't use `await` as you are now, then it'll return `Promise` – choz Dec 24 '20 at 13:27