Wednesday, 7 June 2017

How to add timeout to async await

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

  1. Using async/await which does things in serial
  2. 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
        })
    }))
}

  1. How can I make CheckSitesV2 store successful operations like CheckSitesV1 with time spent?
  2. How can I also store the exception inside the object CheckSitesV1?


via Meanteacher

No comments:

Post a Comment