JavaScript doesn't wait#
Loading data from a server, reading a file or waiting for a timer takes time. If JavaScript stopped and waited, the whole page would freeze. Instead it starts the slow task, carries on, and runs your code later when the result is ready. That is asynchronous programming.
1. Order received 2. Meanwhile, taking the next order 3. Pizza ready (after 1 second)
सोचिए आप restaurant में order देते हैं। Waiter आपका pizza बनने तक खड़ा नहीं रहता - वह दूसरे customers का order लेता है, और pizza तैयार होने पर लाकर देता है। JavaScript भी यही करती है: धीमा काम शुरू करके आगे बढ़ जाती है, और काम पूरा होने पर बाद में आपका code (callback) चलाती है।
The event loop (in short)#
- JavaScript runs your code line by line on one thread (the call stack).
- Slow work (timers, network) is handed to the browser.
- When it finishes, its callback joins a queue.
- When the stack is empty, the event loop takes the next callback from the queue and runs it.
That is why setTimeout(fn, 0) still runs after the current code.
Promises#
Nested callbacks get messy ("callback hell"). A promise is an object that represents a value that will arrive later.
✓ Seat B2-34 confirmed ✗ No seats available on Shatabdi Search finished
resolve(value)→ the promise is fulfilled →.thenruns.reject(error)→ the promise is rejected →.catchruns..finallyruns either way.
async / await#
async/await lets you write promise code that reads like normal top-to-bottom code.
Boiling water... Adding tea leaves... Tea is ready ☕ Served: chai
An async function always returns a promise. Use try/catch for errors:
async function load() {
try {
const data = await getSomething();
show(data);
} catch (err) {
showError(err.message);
} finally {
hideSpinner();
}
}
Running tasks in parallel#
One by one: 600 ms [ 'profile', 'courses' ] together: 300 ms
Promise.all fails if any task fails. Promise.allSettled waits for all and tells you which succeeded.
fetch - loading data from an API#
fetch(url) asks a server for data and returns a promise. Most APIs send JSON. This example loads real sample data from the free JSONPlaceholder API (needs internet):
Sending data uses the same function with options:
const res = await fetch("/api/enquiry", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Asha", course: "JavaScript" }),
});
fetch only rejects when the network fails. A 404 Not Found or 500 still "succeeds" - always check res.ok (or res.status) before using the data.
Summary#
- Async code starts slow work and continues; callbacks run later via the event loop.
- A promise is pending → fulfilled (
.then) or rejected (.catch). async/await+try/catchis the clearest way to write async code.Promise.allruns independent tasks in parallel.fetch+await res.json()loads data from APIs; checkres.ok.
What next? Combine this with the HTML & CSS course: add a dark-mode toggle, a project filter and a contact form check to your portfolio page. Then move on to React or Node.js in the Full Stack program.