Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT Security Toolszitadel v4.18.0(22.09.2026 um 11:25 Uhr)
IT Security ToolsPodroid v1.2.9(22.09.2026 um 12:05 Uhr)
IT Security NachrichtenHow the CIA captured Carlos the Jackal(22.09.2026 um 13:00 Uhr)
Sicherheitslücken (CVE)Aikido Security Unveils Altar-1 Open-Weight AI for Cybersecurity Defense(22.09.2026 um 12:50 Uhr)
Sicherheitslücken (CVE)IT Security News Hourly Summary 2026-09-22 13h : 23 posts(22.09.2026 um 13:00 Uhr)
IT Security NachrichtenHow a Managed SOC works: What happens when a cyberattack begins?(22.09.2026 um 13:02 Uhr)
Sicherheitslücken (CVE)[UPDATE] [mittel] libxml2: Schwachstelle ermöglicht Denial of Service(22.09.2026 um 12:47 Uhr)
IT Security Toolszitadel v4.18.0(22.09.2026 um 11:25 Uhr)
IT Security ToolsPodroid v1.2.9(22.09.2026 um 12:05 Uhr)
IT Security NachrichtenHow the CIA captured Carlos the Jackal(22.09.2026 um 13:00 Uhr)
Sicherheitslücken (CVE)Aikido Security Unveils Altar-1 Open-Weight AI for Cybersecurity Defense(22.09.2026 um 12:50 Uhr)
Sicherheitslücken (CVE)IT Security News Hourly Summary 2026-09-22 13h : 23 posts(22.09.2026 um 13:00 Uhr)
IT Security NachrichtenHow a Managed SOC works: What happens when a cyberattack begins?(22.09.2026 um 13:02 Uhr)
Sicherheitslücken (CVE)[UPDATE] [mittel] libxml2: Schwachstelle ermöglicht Denial of Service(22.09.2026 um 12:47 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

How JavaScript Async Actually Works (Event Loop, Micro tasks, and Call Stack)

If you have ever thought: Why does Promise.then() run before setTimeout? Why does await run “later” even though it looks synchronous? What is actually happening behind async / await? then you are ready to understand how JavaScript asy…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

If you have ever thought:




  • Why does Promise.then() run before setTimeout?

  • Why does await run “later” even though it looks synchronous?

  • What is actually happening behind async / await?



then you are ready to understand how JavaScript async really works.



When I first struggled with “why a Promise is returned”, I realized that I could not go further without understanding the internal mechanism.



In JavaScript, async behavior is built on these five concepts:




  • Call Stack

  • Web APIs

  • Task Queue

  • Microtask Queue

  • Event Loop



Promise and async / await are just built on top of this system.



Let’s break it down step by step.









1. JavaScript Is Single-Threaded



JavaScript can execute only one thing at a time.



Example:




console.log("A")
console.log("B")
console.log("C")






Output:




A  
B
C






This order is controlled by the Call Stack.









2. What Is the Call Stack?



The call stack is:



a stack that keeps track of currently executing functions



It follows LIFO (Last In, First Out).



Example:




function main() {
a()
}

function a() {
b()
}

function b() {
console.log("hello")
}

main()






Execution order:




1 main()  
2 a()
3 b()
4 console.log()






The stack grows as functions are called and shrinks as they finish.









3. The Problem: Long-Running Tasks



Now consider this:




console.log("start")

setTimeout(() => {
console.log("timeout")
}, 0)

console.log("end")






Output:




start  
end
timeout






Why does setTimeout run later, even with 0ms?



This is where Web APIs come in.









4. Web APIs



JavaScript engines (like V8) do not handle timers or network requests.



Features like:




  • setTimeout

  • fetch



are provided by the browser (or runtime environment).



Flow:





  1. setTimeout is called

  2. It is handed off to Web APIs

  3. Timer starts outside the call stack

  4. JavaScript continues execution









5. Task Queue



When the timer finishes, the callback is not executed immediately.



It is placed into the:



Task Queue (Macrotask Queue)



This is a queue of functions waiting to be executed.









6. Event Loop



The event loop is:



a mechanism that checks if the call stack is empty



Flow:




  1. Run everything in the call stack

  2. When empty → take one task from the task queue

  3. Push it to the call stack

  4. Repeat









7. Full Async Flow



Example:




console.log("A")

setTimeout(() => {
console.log("B")
}, 0)

console.log("C")






Execution:




A  
C
B






Why:




  1. A runs

  2. setTimeout registers callback

  3. C runs

  4. call stack becomes empty

  5. event loop pushes task

  6. B runs









8. Promise Is Special



Here is the important part:



Promise.then() does NOT use the normal task queue.



It uses a different queue called:



Microtask Queue









9. Microtasks vs Tasks



JavaScript has two queues:






Task Queue (Macrotasks)




  • setTimeout

  • setInterval

  • DOM events






Microtask Queue




  • Promise.then

  • catch

  • finally

  • queueMicrotask



Key rule:



Microtasks run before tasks









10. Example






console.log("start")

setTimeout(() => {
console.log("timeout")
}, 0)

Promise.resolve().then(() => {
console.log("promise")
})

console.log("end")






Output:




start  
end
promise
timeout






Because microtasks run first.









11. Event Loop Order (Important)



The real execution order:




  1. Run call stack

  2. Run ALL microtasks

  3. Run ONE task

  4. Run microtasks again

  5. Repeat



This is the core rule.









12. What async Really Does






async function test() {
return 1
}






This is actually:




Promise.resolve(1)






So:



async = function that returns a Promise









13. What await Really Does



Example:




async function main() {

console.log("A")

const value = await Promise.resolve(1)

console.log("B")

}

main()

console.log("C")






Output:




A  
C
B






Why?



Because:



await splits the function into two parts using Promise.then









14. Mental Model of await



This:




async function main() {
const value = await getData()
console.log(value)
}






is conceptually:




getData().then(value => {
console.log(value)
})












15. await Splits Execution



Example:




async function main() {

console.log("1")

await Promise.resolve()

console.log("2")

}

console.log("3")

main()

console.log("4")






Output:




3  
1
4
2






Everything after await becomes a microtask.









16. Another Example






console.log("A")

async function test() {

console.log("B")

await Promise.resolve()

console.log("C")

}

test()

console.log("D")






Output:




A  
B
D
C












17. Key Insight About await



await does NOT block the thread



It just:



splits the function and schedules the rest as a microtask









18. Sequential vs Parallel



Sequential:




const a = await fetchA()
const b = await fetchB()






Parallel:




const [a, b] = await Promise.all([
fetchA(),
fetchB()
])






Important:



Parallelism depends on when the Promise starts, not when you await it









19. Why This Matters



If you write:




const a = await fetchA()
const b = await fetchB()






then:




  1. fetchA runs

  2. wait

  3. fetchB runs



But if you write:




const aPromise = fetchA()
const bPromise = fetchB()

const a = await aPromise
const b = await bPromise






both start immediately.









20. Big Picture



JavaScript async is built on:




  • Call Stack → current execution

  • Web APIs → timers, network

  • Task Queue → setTimeout

  • Microtask Queue → Promise

  • Event Loop → orchestrates everything









21. Final Summary



The most important ideas:




  • Promise → runs in microtask queue

  • setTimeout → runs in task queue

  • microtasks always run first

  • await → splits code into microtasks



Once you understand this, you can explain:




  • Why Promise runs before setTimeout

  • Why await runs “later”

  • Why sequential vs parallel happens



And at that point:



async / await stops being magic and becomes predictable.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How JavaScript Async Actually Works (Event Loop, Micro tasks, and Call Stack)

Thematisch verwandte Begriffe: JavaScript, Async, Actually, Works · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94493 | A vulnerability was detected in Gigatech PDV5701 1.0.31_240305_112640. T…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick