0

Consider the following code snippet:

async function f() {
    console.log("entering");
    for (let i=0;i<=1e9;i++) {}
    console.log("quitting");
}

async function g() {
    console.log("before");
    f();
    console.log("after");
}

g();

I would expect the function g() to call f() without waiting for it to finish. Thus, I would expect the output to be

before
entering
after
quitting

However, g() actually waits for f() to finish. Why does this happen, when f() is supposed to be an async function?

6
  • 1
    There is no await within f() that would cause it to run asynchronously. async just means that you can use await within the function. Commented Nov 26, 2019 at 14:44
  • You have to use await . Check this out developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Nov 26, 2019 at 14:45
  • 1
    It seems that you have some misconceptions about what the async/await mechanism means. Commented Nov 26, 2019 at 14:45
  • 1
    @ArmedinKuka — You can only await a promise. A for loop is not a promise. Commented Nov 26, 2019 at 14:54
  • @Pointy This is well possible and is mainly the reason for my question... Commented Nov 26, 2019 at 15:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.