Skip to content

Returning this in net and its inheriting prototype methods #1657

@th507

Description

@th507

One typical way to create a server is through

http.createServer(callback).listen(port);

Then we export the server for other purposes (testing, for example)

module.exports = http.createServer(callback).listen(port);

At this point, everything works as expected.

Later on, when we are trying to add a timeout to the http.Server, we might be tempted to write something like this:

module.exports = http.createServer(callback).listen(port).setTimeout(timeout);

At this time, errors might occur because unlike server.listen, server.setTimeout does not return the instance of http.server.

Event through http module is labelled as stability level 3, but the documentation does not specify the return value for server.listen and for server.setTimeout`. So maybe change the return value is not completely out of the question.

I understand there are multiple setTimeout functions in various modules (like net.setTimeout, message.setTimeout, server.setTimeout, and so on).

Maybe we could consider return http.Server for server.setTimeout(), or even return corresponding instances for all *.setTimeout functions (except the setTimeout in plain JavaScript).

If such changes are not possible, at least we could specify the expected return values for those APIs (namely server.listen, server.setTimeout and some others), so that we might avoid some confusions about those APIs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.httpsIssues or PRs related to the https subsystem.netIssues and PRs related to the net subsystem.semver-minorPRs that contain new features and should be released in the next minor version.tlsIssues and PRs related to the tls subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions