0

I am trying to have a function return the correct values back to another function which does processing on the returned data. I have this function inside the same file as the one I am trying to call it from - a Lambda function - I am calling it using const response = sendRequest(url) which the url is provided from another variable. I've checked this variable and it is for sure the correct value I set it to.

The goal is to return information about the request back to the primary function. I want to process the data there and act on it.

The actual request function should be fine as I can run it exactly as it is - minus the resolve part - in a test.js file.

Problematic function

async function sendRequest (url) {
  let code;
  return new Promise((resolve, reject) => {
    request({ url, timeout: 20000 }, (error, response, body) => {
      if (error == 'ENOTFOUND' || error == 'ETIMEDOUT') {
        response.statusCode = 500;
      }
      if (error || (response.statusCode < 200 && response.statusCode > 299) || !response.statusCode) {
        if (!response || !response.statusCode) {
          code = 500;
        } else {
          code = response.statusCode;
        }
      }
      console.log(`Elapsed time: ${response.elapsedTime}`)
      return resolve({code, error, elapsedTime: response.elapsedTime})
    });
  });
}

Current output

{error: null}
joshk132
  • 1,011
  • 12
  • 37

1 Answers1

-1
const axios = require('axios');

async function sendRequest(url) {
  let code;
  const start = new Date();
  return axios
    .get(url, { timeout: 2000 })
    .then((response) => {
      const elapsedTime = new Date() - start;
      return { code: response.status, error: null, elapsedTime };
    })
    .catch((error) => {
      const elapsedTime = new Date() - start;
      if (error.request.aborted) code = 408;

      return {
        code: code ? code : error.response ? error.response.status : 500,
        error: error.response ? error.response.statusText : error.code,
        elapsedTime,
      };
    });
}

async function main() {
  const response1 = await sendRequest('http://google.com/');
  console.log(response1); // prints { code: 200, error: null, elapsedTime: 97 }

  const response2 = await sendRequest('http://google.com/asdasd');
  console.log(response2); // prints { code: 404, error: 'Not Found', elapsedTime: 48 }

  const response3 = await sendRequest('http://google.comcom');
  console.log(response3); // prints { code: 500, error: 'ENOTFOUND', elapsedTime: 66 }

  // and on timeout it returns { code: 408, error: 'ECONNABORTED', elapsedTime: 44 }

}

main();
Dani
  • 824
  • 7
  • 12
  • Thanks for the response, I will give it a try in the morning. Question about it, would the same apply to the request package I'm using? I ask because while I prefer axios it is lacking in a few response items I need other than just elapsed time. I removed those for the sake of the post but will add them back once I solve the issue of not sending back any useful data – joshk132 Jun 19 '20 at 03:32