@jialin.huang
FRONT-ENDBACK-ENDNETWORK, HTTPOS, COMPUTERCLOUD, AWS, Docker
To live is to risk it all Otherwise you are just an inert chunk of randomly assembled molecules drifting wherever the Universe blows you

© 2024 jialin00.com

Original content since 2022

back
RSS

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.

EOF