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) {
        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".



Read more here: https://stackoverflow.com/questions/64889453/how-to-add-timer-for-js-function-for-some-period-of-time-then-show-message

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: