QA
Beginner LevelNode JS

Compare synchronous (fs.readFileSync) vs asynchronous promise-based (fs.promises.readFile) file operations.

❓ Interview Prompt: Explain node:fs module, synchronous blocking calls (readFileSync), callback API, and promise-based async/await API (node:fs/promises).

The 'node:fs' module provides three execution patterns:

1
Synchronous ('readFileSync'): BLOCKS the entire event loop thread until file operation finishes (Use ONLY during startup initialization!).
2
Callbacks ('readFile(path, cb)'): Legacy asynchronous callback pattern.
3
Promises ('import fs from 'node:fs/promises''): Modern asynchronous non-blocking API designed for 'async/await'!

Code Implementation Example

javascript
import fs from 'node:fs/promises';

async function readAppConfig() {
  try {
    // Non-blocking promise file read
    const data = await fs.readFile('config.json', 'utf8');
    const config = JSON.parse(data);
    return config;
  } catch (err) {
    console.error('Error reading config file:', err);
  }
}
💡Asynchronous non-blocking file reading using node:fs/promises.
🎯 Interview Key Takeaway

Avoid synchronous fs calls (readFileSync) in web request handlers; use node:fs/promises to keep the event loop non-blocking.

#fs#Promises#Async Await#File I/O#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?