4

With node's http library this would look something like

var request = http.request(options);
request.setTimeout(milliseconds, callback);

However, I'm using the wrapper library request and don't see a way to add a callback on timeout.

I'm creating an API endpoint to make an http request and expose that data, and I would like to be able to render a null result if the request times out.

1 Answer 1

6

There is no specific timeout callback. It will be called back as an error; then you have to distinguish it from other errors.

request({
  timeout: 2000,
  url: 'http://timeout.example.org/'
}).on('error', function(err) {
  if (err.code === 'ETIMEDOUT') {
    console.log("Timeout!");
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.