title: Retry nodejs http.request (post,put,delete)
body: <p>What is the correct way to implement a retry on error/condition without using any third party modules in nodejs, please?  </p>
  
  <p>I'm not sure how to call the same function on the error and how to then pass the original callback/data to the newly called function?</p>
  
  <p>Do I need to destroy/end the sockets?</p>
  
  <p>I've tried looking for examples but have only found reference to third party modules and http.get samples which don't seem to work.  How does one test this? 
  I have attempted the below without success:   </p>
  
  <pre><code>      async pingApi(cb) {
          let options = {
          "method":"post",
           "path": `/API/pingAPI?${this.auth}`, /ect do I reference this path?
            }
          };
          let request = await http.request(options, (response) =&gt; {
            let body = new Buffer(0);
            response.on('data', (chunk) =&gt; {
              body = Buffer.concat([body, chunk]);
            });
            response.on('end', function () {
              if (this.complete) {
                let decoded = new Buffer(body, 'base64').toString('utf8')
                let json = JSON.parse(decoded);
                if (json.result != 'OK') {
                  setTimeout(pingApi, 1000); //cant pass callback
                } else {
                  cb(null, json.result) //works
                }
              }
            });
          })
          request.end(); //does the socket need to be closed if retry is this function?
        }
  </code></pre>
  
  <p>Any help, pointing in the right direction or criticism will be greatly appreciated as I think this is a very important learning curve for me.</p>
  
  <p>Thank you in advance,</p>