I have js functions on which I want to apply a timer. so that for some amount of time, the function should execute and after reaching to the set time, the error msg should print like "Time Out".
module.exports.get = function (url) {
return new Promise(function (resolve, reject) {
request({
url: url,
headers: headers,
}, function (err, res, body) {
if (!err && res.statusCode == 200) {
resolve({
status: true,
result: JSON.parse(body)
});
} else {
resolve({
status: false,
result: body
});
}
});
})
}
Now i want is to execute this function for 20 seconds and if the function executed well and get the response then its fine but if it takes more than 20 seconds then it stop the execution and show message saying "Time Out for the request".