From MDN
Asynchronous functions operate in a separate order than the rest of the code via the event loop,
However I don't understand what that means for a simple function that doesn't return anything explicitly and doesn't use await at all. Is it in any way useful to declare a function async in this case? Would it be executed at a later time to allow the page to respond for example while it executes? My test shows it's executed synchronously and doesn't defer at all:
async function foo() {
console.log('Start heavy stuff');
for (let i = 0; i < 90000000; ++i) {
Math.random()
}
console.log('Fnish heavy stuff')
}
foo();
console.log('All done, synchronously');
The logs are shown in the expecterd order, so could there be a use to making this function async in this case? Is this in any way similar to calling this function with a setTimeout(foo, 0)
?
async
functions run synchronously until the first await
or return
is reached, an error is thrown, or the code execution just runs off the end of the function (as it does in your function). At that point, the function returns a promise.
Is it in any way useful to declare a function
async
in this case?
Not in that specific case, no. There doesn't seem to be any reason for the function to return a promise, and all of its work is done synchronously as you saw in your test.
Would it be executed at a later time to allow the page to respond for example while it executes?
No.
There isn't really a good reason to declare a function async
when it doesn't use await
. It makes it return a promise, but doesn't do anything else other than that. Maybe if you were going to use it in a promise chain, but...
Here's a slightly different example of how the async
function runs synchronously:
async function example1() {
console.log("this is done synchronously");
await Promise.resolve();
console.log("this is done asynchronously");
}
console.log("before calling example1");
example1().then(() => {
console.log("promise from example1 was fulfilled");
});
console.log("after calling example1");
That outputs:
before calling example1 this is done synchronously after calling example1 this is done asynchronously promise from example1 was fulfilled
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With