I am trying to add timeout to request function. I know that request library has an option for timeout. Unfortunately it doesn't work very well with proxies. I want to timeout a request without using builtin timeout property of request.
For example this code works
const rp =  require('request-promise');
rp.get(opts)
    .then(function (res) {
        console.log('Body: ' + JSON.stringify(res,null,2));
    })
    .timeout(400)
    .catch(function (err) {
        console.log('Error: ' +err);
    });
If request is not finished in 400 ms, I get error.
I am trying to adopt this strategy to multiple of hosts. I have two options
- Using async/await which does things in serial
- Using Promise.all which does things in parallel.
I am trying to do the requests and record successful operations with the time spent during operation. Serial code kinda works but I couldn't make parallel work. I say kind because I don't know how can I also store the error inside the object. I guess I need to do some check after const resp.. line for possible Error object.
const sitesArray = ['http://www.example.com',
'https://doesnt-really-exist.org',
'http://www.httpbin.org',
'https://httpbin.org/delay/3'];
async function CheckSitesV1() {
    let ps = [];
    for (let i = 0; i < sitesArray.length; i++) {
        let ops = {
            method: 'GET',
            resolveWithFullResponse: true,
            uri:sitesArray[i],
        };
        let time = new Date();
        const resp = await rp.get(ops).promise().timeout(400).catch(e => e);
        let obj = {'time':new Date() - time,'header':resp.headers};
        ps.push(obj);
    }
    return ps
}
function CheckSitesV2() {
    let ps = [];
    for (let i = 0; i < sitesArray.length; i++) {
        let ops = {
            method: 'GET',
            resolveWithFullResponse: true,
            uri:sitesArray[i],
        };
        let resp =  rp.get(ops);
        ps.push(resp);
    }
    return Promise.all(ps.map(function (p) {
        return p
            .then(function (a) {
                // how to store time spent??
                return a.headers;
            })
            .timeout(400)
            .catch(function (e) {
                // how to store time spent??
                console.log('Exception: ' + e);
                return e
        })
    }))
}
- How can I make CheckSitesV2store successful operations likeCheckSitesV1with time spent?
- How can I also store the exceptioninside the objectCheckSitesV1?
via Meanteacher
 
No comments:
Post a Comment