title: Node.js catch and retry on ECONNRESET
body: <p>In my AWS Lambda Node.js code, I've following code that calls the post method to index a document to AWS Elasticsearch service:</p>
  
  <pre><code>var endpoint = 'ABC-XYZ.us-east-1.es.amazonaws.com';
  
  exports.handler = function(input, context) {
      ...
  
      // post documents to the Amazon Elasticsearch Service
      post(endpoint, elasticsearchBulkData, function(error, success, statusCode, failedItems) {
          if (error) {
            console.log('...');
  
            if (failedItems &amp;&amp; failedItems.length &gt; 0) {
              console.log(...);
            }
  
            // NOTE: Instead of failing, we are forcing a success, as we do not want retries
            context.succeed('Success');
          } else {
            // console.log('Success: ' + JSON.stringify(success));
            context.succeed('Success');
          }
        });
      }
  
     ...
     ...
  function post(endpoint, body, callback, lastTimeout) {
    lastTimeout || (lastTimeout = 500);
    var requestParams = buildRequest(endpoint, body);
  
    var request = https.request(requestParams, function(response) {
      var responseBody = '';
      response.on('data', function(chunk) {
        responseBody += chunk;
      });
      response.on('end', function() {
        var info = JSON.parse(responseBody);
        var failedItems;
        var success;
  
        if (response.statusCode &gt;= 200 &amp;&amp; response.statusCode &lt; 299) {
          failedItems = info.items.filter(function(x) {
            return x.index.status &gt;= 300;
          });
  
          success = { ...};
        }
  
        var error = response.statusCode !== 200 || info.errors === true ? {
          "statusCode": response.statusCode,
          "responseBody": responseBody
        } : null;
  
        callback(error, success, response.statusCode, failedItems);
      });
    }).on('error', function(e) {
      console.error(e.stack || e);
      //callback(e);
      lastTimeout *= 2;
      console.log('lastTimeout: ' + lastTimeout + " for cluster: " + endpoint) ;
      setTimeout(function() {
        post(endpoint, body, callback, lastTimeout);
      }, lastTimeout);
    });
    request.end(requestParams.body);
  }
  
  ...
  </code></pre>
  
  <p>At times I get <code>Error: socket hang up ECONNRESET</code>. </p>
  
  <p>My question is: <strong>What would be the best way to catch this error and retry?</strong></p>
  
  <p>I added the <code>setTimeout</code> snippet based on <a href="https://stackoverflow.com/a/29149852/4039725">this answer</a> and it looks like it does work but I'm not sure if that's the right way to do.</p>
  
  <p>Node.js version is 4.3. </p>
  
  <p>I was thinking of using <code>Promise</code> with <code>resolve</code> and <code>reject</code> but being a JS Newbie, I'm not sure how to make use of promise in my post call. </p>
  
  <p>I also went through <a href="https://dev.to/ycmjason/javascript-fetch-retry-upon-failure-3p6g" rel="nofollow noreferrer">this link</a> but not clear on how can I wrap my post call with <code>fetch_retry</code></p>