the syntax sugar: async/await is blocking?
Async functions are a common use in JS, but there's confusion about whether they're truly "non-blocking.”
async function getData() {
const data = await fetch('/api'); // Pauses this function, but doesn't block the main thread
// do something here - this definitely waits for fetch to complete
}
getData(); // Continues executing the code below immediately
console.log('This line runs right away, no waiting for fetch'); // Executes immediately
For the "do something here" part, it still has to wait for the await to finish before it can run. But for the main program, this isn't blocking.
Within the function: still blocking
For the main thread: not blocking
So when we say "async is non-blocking," it's all about the main thread, not the line that comes after await.