Skip to content Skip to sidebar Skip to footer

Undocumented Response.finished In Node.js

Just starting off with node.js and following Node Cookbook but I am stuck on URL routing part. here is the code from the book itself: var http = require('http'); var path = require

Solution 1:

As of this date, response.finish is documented (actually per the doc it was introduced in v0.0.2)

From the Node API Official Documentation,

response.finished

Added in: v0.0.2

Boolean value that indicates whether the response has completed. Starts as false. After response.end() executes, the value will be true.

Solution 2:

It's declared here and set here. TL;DR: when response.end() is (almost) done, the finished property is set to true (response is an instance of http.OutgoingMessage or a subclass).

I actually have to disagree with the comment about it not being a race condition, because I think it is. Even though forEach isn't asynchronous itself, in this case it calls asynchronous functions (writeHead() and end()). If those functions take a bit of time, the check for response.finished might be called too soon and you'll end up with an error.

Also, I wonder if finished should be used at all. It's not documented and not exposed to the outside world through a method. In other words, it might work now but there's no guarantee it will keep working in the future.

Post a Comment for "Undocumented Response.finished In Node.js"