Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]

29,754

I finally found the solution to my problem.

When using request module for calling REST APIs , you need to specify pool variable in the options. This serves as maxsockets, which is a declaration for concurrent request processing.

Sample code is posted below for those who run unto this issue:

For more info, check out below post: How to use Request js (Node js Module) pools

var separateReqPool = {maxSockets: 20};
var request = require('request');
var url_array = ['url1','url2','url3'];//Array of all the urls to call


async.map(url_array, function(item, callback){
      request({url: item, pool: separateReqPool}, function (error, response, body) {
          //Do Something with the response
          });
        }, function(err, results){
          console.log(results);
        });
     }).
     catch((err) => {
        console.log(err);
 }); 
Share:
29,754
Pumpkin Pie
Author by

Pumpkin Pie

Updated on January 25, 2020

Comments

  • Pumpkin Pie
    Pumpkin Pie over 4 years

    I'm trying to call REST API asynchronously in a for loop using XMLHttpRequest module. I'm making 400 requests in 1 loop, with a wait time of 1 sec after every 100 requests. This works fine on clientside JavaScript. However, when I run on NodeJS using the same module (it's not natively available, I had to download from npmjs) , I'm getting this error after about 230 requests. Any idea if there is another module that I can use to better handle this bulk API requests?

    Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]

  • Ashwanth Madhav
    Ashwanth Madhav over 4 years
    Getting error like { Error: connect ETIMEDOUT xxx.xxx.xx at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1082:14) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect', address: xxx.xxx.xx, port: 3010 }