0

I am trying to understand node.js streams. Since the write method on the writer object method is asynchronous, can counter value be out of order?

const writer = fs.createWriteStream(path.resolve("modules", "streams", "hello"));
writer.on("finish", () => console.log("Finished Writing"));
for (let i = 0; i < 1000; i++) writer.write(`Hello:${i}`); // this is async, according to me
writer.end();
console.log("Hello");

Outputs: 
Hello
Finished Writing
Rajat Aggarwal
  • 392
  • 3
  • 16

1 Answers1

1

No, the counter won't be out of order. Streams insert items into the outgoing buffer as you call .write() and all your writes are called in order.

But, a loop like this needs flow control on the writer.write() because if it returns false, then you can't write any more until the drain event on the stream indicates that there is more room for writing. See here in the doc for more info.

The write.write() function is asynchronous, but because of buffering and other events on the stream you don't necessarily have to register a complete callback for every write. The write.write() puts data in the outbound buffer and returns true immediately when the data has been successfully buffered or it returns false immediately if there's no room in the buffer.

The actual write to the file stream is indeed asynchronous and occurs largely out of your view. Errors from the asynchronous part are communicated via the error event on the stream and, as you're already doing, completion of the stream is also communicated via an event.

The reason you get Finished Writing after Hello is because of the asynchronous writing behind the scenes. Your for loop sets off the writes, but they are not yet complete when the for loop is done. They finish some time later (when you see the finish event).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I am not getting error while writing, although writer.write() returning false.Why? – Rajat Aggarwal May 23 '21 at 02:47
  • 1
    @RajatAggarwal - If you read the [doc for `.write()`](https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback), it is all explained there. It will try to avoid an error, by consuming larger amounts of memory, causing excess GC and perhaps even aborting. – jfriend00 May 23 '21 at 02:54
  • 1
    @RajatAggarwal - If you know you're not going to be writing a lot of data, you can get away without paying attention to the boolean return value and the drain event. – jfriend00 May 23 '21 at 03:31
  • I was just learning about streams in node, got to know more about them, thanks. – Rajat Aggarwal May 23 '21 at 05:03