Hungry Web Developer Podcast
Speed
Volume
When you execute asynchronous code in Javascript, it goes through something called an Event Loop. It describes in what order your code will run by using a call stack and a callback queue. This is important to understand so you don't run into race conditions in your codebase. In this episode, we deep dive into how it all works!
- Basics and why it matters
- The mechanics
console.log("hello");
setTimeout(() => {
console.log("resolve timeout");
},5000)
console.log("last line");
Hello
is logged, then resolve timeout
is placed into a callback queue. last line
is then logged. 5 seconds later, the callback queue is checked, and resolve timeout
is logged.
What if you had a long list of executable code, and a setTimeout
with a very short duration called in the middle?
console.log(1);
console.log(2);
console.log(3);
setTimeout(()=> {
console.log("set time out function")
}, 100);
//.....
console.log(1000);
The setTimeout
function will still execute after all other code runs.
- Additional notes
Promise.all()
- Dessert Time
Download: the_event_loop.mp3