Practice & Assessment
Test your understanding of Async JavaScript in Node.js
Multiple Choice Questions
5What is the difference between Promise.all and Promise.allSettled?
Why does awaiting inside a for loop run iterations sequentially instead of in parallel?
What is the recommended action inside a process.on('uncaughtException') handler?
Which code snippet correctly runs three file reads in parallel using async/await?
What does emitter.once('event', handler) do differently from emitter.on('event', handler)?
Coding Challenges
1Async File Merger with Error Handling
Write an async function mergeTextFiles(inputPaths, outputPath) that reads all files listed in inputPaths in parallel using Promise.all, concatenates their contents separated by a newline, and writes the result to outputPath. Handle the case where any input file does not exist by logging a warning and skipping that file without failing the whole operation. Input: array of file paths and an output path string. Output: written file and a console summary showing how many files were merged vs skipped. Estimated time: 25 minutes.
Mini Project
Event-Driven File Processor
Build a FileProcessor class that extends EventEmitter. It must: accept a directory path in the constructor; expose a start() method that reads all .txt files in that directory; emit a 'file' event for each file with {name, content, wordCount}; emit a 'done' event when all files are processed with a summary {totalFiles, totalWords}; emit an 'error' event if the directory does not exist or a file cannot be read. Process files in parallel using Promise.all. Write a script that creates a FileProcessor, registers listeners for all events, and calls start(). Test with at least three .txt files in a directory.
