Writing and Reading file async
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Node.js, asynchronous file I/O is usually the right default because it avoids blocking the event loop while the operating system reads or writes data. The modern API for this is fs/promises, which works naturally with async and await. For small and medium files, readFile and writeFile are simple and effective. For large files or streaming workflows, streams are often a better fit.
Read a File Asynchronously
Using fs/promises, asynchronous reading looks like this:
The important point is that await readFile(...) suspends the async function without blocking the entire Node.js process.
If you want raw bytes instead of text, omit the encoding and you will get a Buffer.
Write a File Asynchronously
Writing is similar:
This writes the full content asynchronously and resolves when the operation finishes or rejects if it fails.
Read and Then Write in One Flow
A common pattern is to read a file, transform it, then write another file.
This keeps the control flow linear and readable without callback nesting.
Handle Errors Explicitly
File operations fail for ordinary reasons:
- the file does not exist
- permissions are wrong
- the path is invalid
- the disk is full
Use try and catch inside async functions:
Async I/O still needs normal error handling. await does not remove failure cases.
Use Streams for Large Files
readFile and writeFile load or write the whole payload at once. For very large files, streams are often more memory-efficient.
Streaming is a better model when:
- files are very large
- you want chunk-by-chunk processing
- you are forwarding data rather than holding it all in memory
Common Pitfalls
The biggest mistake is forgetting await, which means the code continues before the file operation has completed.
Another common issue is mixing callback-based fs.readFile with promise-based fs/promises code in the same flow without a clear reason. Pick one style and keep it consistent.
Some developers also use synchronous APIs such as readFileSync in server code without realizing they block the event loop. That can hurt concurrency under load.
Finally, for large files, do not assume readFile is always the best answer. Simpler does not always mean more scalable.
Summary
- In modern Node.js, use
fs/promiseswithasyncandawaitfor async file I/O. - '
readFileandwriteFileare ideal for straightforward small and medium file operations.' - Use
tryandcatchfor predictable error handling. - Prefer streams for very large files or chunked processing.
- Async file APIs keep the event loop free, but they still need careful control flow and error handling.
Related reading
- writing tfrecord with multithreading is not fast as expected
- Writing to a TextBox from another thread?
- WSGI vs ASGI server with hybrid sync/async Django app
- Xamarin Gcm Network Manager await httpclient
- XMLHttpRequest used to find quicker server
- Yarn v2 gitignore
- Xamarin.Android no stack trace in async method
- XCTest NSURLSession Stall on main thread
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.