📌Intro
If await
pauses execution… doesn’t that make it synchronous again? This is the #1 async misconception.
đź§ Context
-
await
looks synchronous but isn’t. - It pauses only the current async function, without blocking the event loop.
Examples:
async function main() { console.log("Before"); await new Promise(res => setTimeout(res, 1000)); console.log("After"); } main(); console.log("Outside"); // Output: // Before // Outside // After
Arrow version:
const main = async () => { console.log("Start"); await new Promise(res => setTimeout(res, 1000)); console.log("Done"); }; main();
đź’ˇReal-Life Example:
Your UI doesn’t freeze while waiting for a network call.
E.g., while checking login, users can still scroll, click, or type.
JavaScript is single-threaded, but async/await
lets you write non-blocking code that looks clean.
Top comments (0)