Synchronous vs. Asynchronous Code
Synchronous code runs one statement at a time, and each line waits for the previous one to finish. Asynchronous code starts a task, such as reading a file or calling an API, and lets the program move on while that task completes in the background. Knowing the difference explains why a Node server can handle many requests at once and why a value you expected is sometimes undefined.
One thread, one call stack
JavaScript runs your code on a single thread. Function calls are pushed onto the call stack and popped off when they return. While a function is running, nothing else in your JavaScript can run. If that function takes two seconds, the whole program is frozen for two seconds. In a browser the page stops responding to clicks; in an Express server every other request waits in line.
Slow operations like disk access, network calls and timers are handed off to the runtime (the browser or Node's libuv layer). When the operation finishes, a callback is placed in a queue. The event loop picks that callback up once the call stack is empty.
Seeing the order for yourself
This small Node script shows that asynchronous callbacks run after the current synchronous code, even with a delay of zero.
const fs = require('node:fs');
console.log('1. start');
setTimeout(() => console.log('4. timer fired'), 0);
fs.readFile(__filename, 'utf8', (err, text) => {
if (err) throw err;
console.log('5. file read, length:', text.length);
});
console.log('2. still running');
console.log('3. end of script');
Lines 1, 2 and 3 always print first. The timer and the file callback print later, because they had to wait for the stack to clear. The exact order between the timer and the file can vary, which is another reminder that you should never depend on timing between unrelated async tasks.
Blocking versus non-blocking APIs in Node
Many Node modules offer both styles. The table compares them.
| Synchronous (blocking) | Asynchronous (non-blocking) |
|---|---|
fs.readFileSync(path) | fs.readFile(path, cb) or fs.promises.readFile(path) |
| Returns the result directly | Delivers the result through a callback or promise |
| Stops all other work until done | Lets other requests run meanwhile |
| Fine for startup scripts and config loading | Right choice inside route handlers |
Reading a config file once when the server boots is fine to do synchronously. Reading a file on every request with readFileSync means one slow disk read holds up every visitor.
The classic mistake: returning a value from a callback
Beginners often write a function that starts an async task and tries to return its result:
function getConfig() {
let data;
fs.readFile('config.json', 'utf8', (err, text) => {
data = JSON.parse(text);
});
return data; // undefined: the callback has not run yet
}
The return runs immediately, long before the file is read. The callback assigns data later, when nobody is looking. The fix is to pass the result forward: accept a callback, return a promise, or use async/await. The next lessons cover these patterns.
Takeaway
Synchronous code is simple to read but blocks the single thread. Asynchronous code keeps the program responsive but delivers results later, through callbacks or promises. Whenever a function talks to the disk, network or a timer, assume the result arrives later and design your code to wait for it rather than expecting it on the next line.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.