How to add timer for js function for some period of time then show message

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) {
            url: url,
            headers: headers,
        }, function (err, res, body) {
            if (!err && res.statusCode == 200) {
                    status: true,
                    result: JSON.parse(body)
            } else {
                    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".

Read more here:

Content Attribution

This content was originally published by Lalit Dubey at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: