So, I'm making this request to a server and I set a timeout and I want to handle the timeout event, but I also want to handle the 'abort' event and differ one for another. I managed to do it with a quick fix, but I wanted to know if there is a better way to do it. The code looks like this:
makeRequest = function(json, cb){
var requestError
request({
url: REQUEST_URL,
json: json,
timeout: 3000,
headers: {
'Content-Type': 'application/json'
}
}, function(err, res, body){
if(err) requestError = err
else cb(null, res, body)
}).on('abort', function(){
setTimeout(function({
if(requestError != 'ETIMEDOUT') cb(httpStatusCode.REQUEST_TIMEDOUT)
else cb(httpStatusCode.REQUEST_ABORTED
}, 1000)
})
}
I noticed that in the timeout event both the 'abort' event is triggered and the request callback is called, in this order, so I used the setTimeout function to wait for the request callback and than handle the error in the 'abort' listener. This seems like a dumb way to do it and I searched online and did not find a way yo handle only the callback event. I also noticed that the timeout triggers the on.('error', function(err){}) event, where I can handle the error, but it also calls the on.('abort', function(){}) event and I end up calling the main callback (cb) two times, crashing my application.
Is there a way I can have an event ONLY for the timeout and one ONLY for the abort, so I don't have to use setTimeout?
Or is there any property in my req object that I can check to see if that request timed out or not?
Or do you have any other suggestion to fix my problem in a less ugly way?
I'm using nodejs 0.12.2 and request 2.55.0 Thanks!