I haven't used JavaScript for a long time, and now I'm trying to grasp the meaning of async/await keywords. I have found and read the tutorial on MDN (https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) which left me with two questions.
- The tutorial says when I want to call an async function inside another async function, I have to use
await. Why?
If I understand correctly async makes function runable into another thread. So every time I call a function marked with this keyword I make a new thread and get a Promise for this thread. If I can't do it inside async function does it means a thread in JavaScript can't spawn another thread? It seems like a browser can only run two threads: one main thread and one and only one extra thread. This makes all that fancy multithreading ability in JS a little bit thin.
awaitcan't be used outside of async function. Again: why?
I searched StackOverflow and I've found this question was answered elsewhere, but I want to know if something have changed in this matter for nowadays. I see this rather strange why I can't use await in the main thread. Why I can't wait for something? This makes me enclosing everything that must use some async functions into another async function every time. I know there may be an issue that, for example a code waiting for a long process loading something from the Web could make the rest of the webpage suspended for user interactions. But what about a callback for a button click? To use fetch() I have to make the callback async. But a callback can't be async, right?

async/awaitare not related to it. "for example a code waiting for a long process loading something from the Web could make the rest of the webpage suspended for user interactions."awaitdoesn't block the thread. Quite the opposite it pauses the current execution to allow other tasks to run.await" you don't HAVE to. Only if you want to align your code in a way that lines make sense consequitively. You can fire an async function without awaiting it if you don't need the result. You can also mix it with promises but it's usually a bad idea only becauseawaitis syntactic sugar over promises and it's better to keep to one async abstraction.