1

The following code:

function log() {
    console.log('ok');
}
function log2() {
    console.log('ok2');
}

async function run() {
    await log();
    await log2();
}

run();

console.log(1);

Returns:

ok
1
ok2

The question is: being "run" an async function shouldn't it execute after all synchronous calls? (Such as console.log(1)). Actually the one that executes after the sync call is the second await.

The return value should be:

1
ok
ok2

Why does the first await executes synchronously?

3 Answers 3

4

being "run" an async function shouldn't it execute after all synchronous calls?

No.

Marking a function with the async keyword:

  • Makes it return a promise
  • Allows the use of the await keyword inside it

It does not make synchronous code asynchronous. It does not move code to another thread. It does not immediately pause the execution of the async function.


So when you call run, it calls log, which calls console.log (ok), then returns undefined. The await keyword causes run to go to sleep until the returned promise settles.

The main part of the program continues running and it logs 1.

The promise settles immediately (because it is undefined and not a promise), so run wakes up and calls log2.

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

2 Comments

Your answer actually made me go back and read the spec. I wasn't clear on the fact that await is what actually does the promisifying. The difference is subtle, but important as it means the difference between a normal function returning Promise.resolve(undefined) vs just undefined. Could you check my answer and tell me if I am clear now?
@zero298 — No. The async keyword makes the function return a promise. It is await that makes the function go to sleep so other code can run.
2

This is my understanding of it. async/await is just syntactical sugar for Promises. Your logs aren't actually async and they return undefined.

However, since you say await log() that turns into Promise.resolve(log()) and the code after is put into a .then().

From MDN:

If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise.

Whenever you return the Promise you "defer" until other synchronous stuff is done, much like a setTimeout(fn, 0) would do. Because of that, you don't see "ok2" until after the synchronous portion is complete.

That's the other important bit. It won't run the rest of the asynchronous stuff because it has been put into a callback queue and that isn't processed until the synchronous stuff is done. See Concurrency model and the event loop for a deeper explanation of how things are handled "later".

function log() {
    console.log('ok');
}
function log2() {
    console.log('ok2');
}

async function run() {

  //await log();
  /*
   * log is called immediately and the await wraps the
   * return value in a resolved Promise, the next await
   * statement will wait for the resolved Promise which
   * pushes the callback execution to the internal runtime queue
   */
  Promise.resolve(log())
  // await log2()
  /*
   * This won't get executed until the synchronous runtime
   * runs out of stuff to do.  Once it does, this will get
   * executed
   */
    .then(() => Promise.resolve(log2()));
}

run(); // Synchronous

console.log(1); // The other synchronous stuff to do

1 Comment

Clear as water, Is there any documentation explaining this phenomena?
-2

Async is something happening behind the scenes. That means if a code finishes executing, it outputs or returns. On the other hand, the synchronous code will run as usual. In the above code, you are console logging a text. That doesnt require much time to execute. So its outputted just as it finishes. Try using setTimeout in async code and you can see the synchronous code execute first

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.