Skip to content

Support timeout for request #15

@haquezameer

Description

@haquezameer

With the current system, a req is retried infinitely until we get a success response. Accepting a timeout would be good to break the loop of retrying.

Faced this recently when writing a few jest tests, where I wanted the request to succeed but only wait for a certain time and let the request timeout after that.

Something like this?

    const main = () => {
      const request = http.request(
        { method, port: options.port, path: options.path, timeout: 5000 },
        (response) => {
          if (response.statusCode === 200) {
            return resolve({ started: true });
          }

          return main();
        },
      );

      request.on('error', main);
      request.on('timeout', () => {
        request.destroy();
        return resolve({ started: false });
      });
      request.end();
    };

    main();

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions