Asnyc functions work asynchronously but javascript is single-threaded, which means the browser can do only one task at a time. What you do inside the function must be asynchronous by nature to actually make it work, however you probably did some blocking operation there that is synchronous by nature. Let me explain in more details by giving example:
async function fooThatCanBeAsync(){
var result = await fetch("https://stackoverflow.com");
console.log("after async task 1");
return result;
}
async function fooThatCannotBeAsync(){
var x = 0;
for(i=0; i<10000000; i++){
x += i;
}
console.log("after async task 2");
}
fooThatCanBeAsync();
fooThatCannotBeAsync();
console.log("end...");
The above silly example has two async functions. However, only one of them can run async!
The first function tries to fetch something by sending http request and it will take some time. This means, cpu is now free until it starts receiving the result. By making this function async, you can now use this time in between to do other tasks. This is how concurrency works in javascript.
The second function does a for loop, making some calculations. Those calculations cannot be async, because there is no way to interrupt it, no time to wait in between, and no i/o operation. Just because you add async keyword, you can't change the fact that only one cpu thread is executing that code.
longAsyncWork? It sounds like the work is not actually asynchronous?awaitin the functionfoofunction is doing async work?foodo expensive synchronous processing before it resolves?