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}