title: NodeJs - Is there a way to limit number of call/tries that are made when trying to connect to a remote server using await & Promise
body: <p>So I am facing a issue where in I am trying to connect to a remote server in Nodejs using async, await and Promise.  </p>
  
  <pre><code>Error: connect ETIMEDOUT 10.1.239.44:80
       at Object._errnoException (util.js:1022:11)
       at _exceptionWithHostPort (util.js:1044:20)
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
  </code></pre>
  
  <p>The issue is that when the server is unavailable and above error occurs, instead of returning the status, my request keeps trying to connect to the server periodically as seen below - </p>
  
  <pre><code>  Do the POST call
    ERROR&gt;&gt;&gt;&gt;Error: connect ETIMEDOUT 10.1.239.44:80
    Do the POST call
    ERROR&gt;&gt;&gt;&gt;Error: connect ETIMEDOUT 10.1.239.44:80
    Do the POST call
    ERROR&gt;&gt;&gt;&gt;Error: connect ETIMEDOUT 10.1.239.44:80
    Do the POST call
    ERROR&gt;&gt;&gt;&gt;Error: connect ETIMEDOUT 10.1.239.44:80
    Do the POST call
    ERROR&gt;&gt;&gt;&gt;Error: connect ETIMEDOUT 10.1.239.44:80
  </code></pre>
  
  <p>Below is my code snippet</p>
  
  <p>Front end (Reactjs)- </p>
  
  <pre><code>submit = async() =&gt; {
  this.spinner("show");
  
  var fileInput = document.getElementById('file-input');
  
  if(fileInput.files.length === 0){
      alert("Select at least one file to upload.");
  }else{
      for (var i = 0; i &lt; fileInput.files.length; i++) {
          const upDoc = await this.uploadDocument(fileInput.files[i].name)
        .then(document =&gt; {
          this.setState({ document: document.document })
          this.displayDocumentInfo();
  
          this.spinner("hide");
          this.reload();
          this.setState({ uploaded: !this.state.uploaded });
        })
        .catch(err =&gt; {
          this.spinner("hide");
          alert(err);
          console.log("ERROR111&gt;&gt;&gt;&gt;"+err);
        });
  
        console.log("NI Nmber&gt;&gt;&gt;"+this.state.document["NI Number"]);
  
      }
  }
  }
  </code></pre>
  
  <p>Back end(NodeJs) - </p>
  
  <pre><code> app.get('/uploadDocument/:appId/:file', async (req, res)  =&gt; {
       var document  = await datacapconnection.uploadDocument(req.params.file,req.params.appId)
                        .then(document =&gt;{
                          console.log("DOCUMENT &gt;&gt;&gt; "+JSON.stringify(document));
                          var jsondata = JSON.parse(document);
  
                          if(jsondata.hasOwnProperty('DocId')){
                            var docId = jsondata.DocId;
                            var financeInfo  = dbconnection.saveFinacialInfo(document);
                            console.log("Finance Info &gt;&gt;&gt;"+JSON.stringify(financeInfo));
                            var docInfo = dbconnection.insertDocumentInfo(docId,req.params.appId,req.params.file);
                          }
  
                          res.json({ document: JSON.parse(document) });
                        })
                        .catch((err) =&gt; {console.log("ERROR&gt;&gt;&gt;&gt;"+err)});
  });
  </code></pre>
  
  <p>From the above code I am able to get the error from the function <strong>datacapconnection.uploadDocument</strong> and print it in the <strong>catch</strong> block.</p>
  
  <pre><code> const express = require('express');
   var http = require('http');
  
   const app = express();
   var request = require('request');
  
   var FormData = require('form-data');
   var fs = require('fs');
  
   const dirName = 'C:\\Users\\abhinav.a.mehrotra\\Desktop\\DataCap\\PaySlips\\';
  
  var uploadDocument = function(file, appId){
  
    return new Promise((resolve, reject) =&gt;{
  
     jsonObject = JSON.stringify({
      "file" : file
  });
  
  var postheaders = {
    'Content-Type' : 'Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryyrV7KO0BoCBuDbTL'
  };
  
  const formData = {
    file: fs.createReadStream(dirName + file),
  };
  
  var payload = {
    url: 'http://10.1.239.45/datacapture/fn/digidocs/submitAsTransaction/'+appId,
    formData: formData,
    headers: postheaders
  }
  
  console.log('Do the POST call');
  
  var proxyRequest = request.post(payload, function(err,res,body) {
    if(err){
      reject(err);
      return
    }
  
  
        resolve(body);
  });
   })
  }
  
   exports.uploadDocument = uploadDocument;
  </code></pre>
  
  <p>How can i make sure that only one call goes out. If there is any error it returns to my front end rather than keep waiting for the connection to happen. Any Help is much appreciated.</p>