JavaScript is single-threaded - imagine a single worker who can only do ONE task at a time.
console.log("Task 1");
console.log("Task 2");
console.log("Task 3");
// Executes line by line, top to bottom
Some operations take time:
If JavaScript waits for these, everything freezes:
// ❌ BAD - Everything stops here
let data = waitForAPI(); // Blocks for 3 seconds
console.log(data);
console.log("This waits 3 seconds too!");
Don't wait - instead, say "Call me back when you're done":
// ✅ GOOD - Non-blocking
fetchAPI(function() {
console.log("Data received!");
});
console.log("I run immediately!");
// Output:
// I run immediately!
// Data received! (after delay)
This "call me back" function is a CALLBACK.
Think of ordering food online: