I am modifying a function in nodejs 14 in aws lambda, The code will fetch a json and then do other tasks:
const https = require('https');
var http = require("https");
var AWS = require('aws-sdk');
exports.handler = async (event, context, callback) => {
console.log('Event: ', JSON.stringify(event, null, 2));
const request = event.Records[0].cf.request;
const https = require('https');
const http_get = require('https');
var options = {
host: 'www.local.dev',
method: 'GET',
timeout: 2000,
path: '/list.json',
headers: {
'Accept': 'application/json'
}
};
const res = await new Promise(resolve => {
http_get.get(options, resolve);
});
let data = await new Promise((resolve, reject) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('error', err => reject(err));
res.on('end', () => resolve(data));
});
data = JSON.parse(data)
// CONTINUE FUNCTION
return request;
};
I would like to set a timeout if the request to fetch the json doesn't return in 2 seconds., I continue the function, The function has other parts, so even if nothing comes from the json endpoint it can continue.
It has no dependency on the json return.
What is happening and that the timeout is not validated and the function is running indefinitely.
I need to use this http module from node.js, I can't use axios here.
To simulate the timeout I am using this app: httpstat.us/501?sleep=60000