Questions
16 of 24
1What are streams?
2List elementary stream types.
3What are advantages of streams.
4Create a readable stream
5How can you create a custom Transform stream in Node.js?
6What is the `end` event in a readable stream, and how is it different from the `finish` event in a writable stream?
7How can you handle errors in Node.js streams?
8Explain the concept of object streams and provide an example use case.
9Explain the difference between `ObjectMode` and `Buffer` mode in a readable stream.
10Explain the difference between a flowing and a non-flowing readable stream.
11What is the difference between the `data` event and the `readable` event in a readable stream?
12How can you handle cleanup operations when working with streams?
13What is the purpose of the `highWaterMark` option when creating a readable stream?
14What is backpressure in Node.js streams, and how can you handle it?
15What is the pause() method used for in a readable stream, and how can you resume data flow?
16What is the purpose of the `stream.finished()` utility in Node.js?
17How do you handle stream errors when working with async/await in Node.js?
18How can you handle memory usage when dealing with large files in Node.js streams?
19Explain the concept of piping in Node.js streams.
20What is the purpose of pipeThrough function
21What is the purpose of the `unpipe()` method in Node.js streams, and how does it work?
22Explain the difference between `pipe()` and `on('data')` when working with streams.
23What is the purpose of the `stream.pipeline()` function in Node.js?
24write code of a stream to read a local file in node js
16 / 24

What is the purpose of the `stream.finished()` utility in Node.js?

Difficulty: 5/10
stream lifecycle, error handling, resource cleanup

stream.finished()` returns a Promise that resolves when a stream has finished processing or rejects if an error occurs. It provides a convenient way to handle stream completion in an asynchronous and Promise-based manner.

javascript

Scenario Questions

0-2 years experience

  1. 1You need to read a file and pipe it to an HTTP response. How would you use stream.finished() to know when the response has fully sent, and why is that better than just listening to the 'end' event?
  2. 2If you forget to attach an 'error' listener to a readable stream, what can go wrong, and how does stream.finished() help you catch that situation?
  3. 3Write a short snippet that uses stream.finished() to log a message when a writable stream either finishes successfully or encounters an error.

2-5 years experience

  1. 1Our microservice streams JSON to a client, but sometimes the client disconnects early, causing an ECONNRESET. How would you modify the code with stream.finished() to ensure resources are cleaned up and the error is logged?
  2. 2A background job that processes large CSV files started hanging after a recent deploy. Explain how you would use stream.finished() to determine whether the stream is completing or stuck, and what you would look for in the callback.
  3. 3You need to add a retry mechanism for a network stream that may fail mid‑transfer. Where would you place the retry logic in relation to stream.finished(), and what edge cases must you guard against?

5-8 years experience

  1. 1Design a high‑throughput log aggregation service that consumes streams from many sources and writes to a central store. Discuss how you would incorporate stream.finished() to manage backpressure, detect completion, and ensure graceful shutdown without losing data.
  2. 2Our system must support hot‑reloading of stream‑processing pipelines. Explain how you would use stream.finished() to safely replace a running pipeline component while guaranteeing that in‑flight data is either processed or properly aborted.
  3. 3When scaling the streaming component horizontally, you notice occasional memory leaks after streams finish. How would you investigate whether stream.finished() callbacks are causing the leak, and what patterns would you adopt to avoid it?

8+ years experience

  1. 1We are migrating a legacy Node.js service that manually attaches 'close' and 'error' listeners on streams to a new architecture that relies on stream.finished() across multiple modules and teams. What architectural guidelines, testing strategies, and cross‑team conventions would you put in place to ensure consistent error handling and resource cleanup at scale?
  2. 2In a multi‑tenant SaaS platform, each tenant's data is streamed through a shared processing layer. How would you design a contract using stream.finished() to enforce tenant isolation, prevent one tenant's stream errors from affecting others, and provide observability for operations?

Follow-up Questions

  • What would happen if you called stream.finished() without providing an error argument in the callback?
  • Can you describe a case where the 'close' event fires but stream.finished() does not, or vice‑versa?
  • How does using stream.finished() affect the way you structure promise‑based code around streams?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.