Skip to content
Advertisement

How do I wait for multiple promises to resolve but also include a set minumum delay?

I am calling two APIs with two different functions and having a setTimeout for both, is it possible to handle their timeouts in such a way that they finish in 10s instead of 15s.

function delay(s){
    return new Promise(resolve => setTimeout(resolve,s));
}

async function getXml(){
    let ans;
    await delay(10000)
    const {data} = await axios.get('https://gist.githubusercontent.com/SwayamShah97/a3619c5828ac8ed8085c4ae295a855d9/raw/e4e372552e042bd8bd9e8ab87da93eb030114f86/people.xml');
    xml2js.parseString(data, (err, result) => {
        if(err) {
            throw err;
        }
        ans = result
        
    });
    return ans;
    }

async function getPeople(){
    await delay(5000)
    const { data } = await axios.get('https://gist.githubusercontent.com/SwayamShah97/0f2cb53ddfae54eceea083d4aa8d0d65/raw/d7d89c672057cf7d33e10e558e001f33a10868b2/people.json');
    return data; // this will be the array of people objects
}

Is there a way to run this code only in 10s, so that both the APIs get called in 10s time period

Advertisement

Answer

Forcing either one or both axios.get() functions to complete below some time limit (other than failing on a timeout), isn’t doable, since you don’t control the transport.

One thing you can do is force one or more functions to complete at or after some time threshold, with something like this…

function padToTime(promise, interval) {
  // delay returns a promise that resolves after an interval
  const delay = interval => new Promise(resolve => setTimeout(resolve, interval));
  // caller can provide a singular or an array of promises, avoiding the extra .all
  let promises = Array.isArray(promise) ? promise : [promise];
  return Promise.all([...promises, delay(interval)])
    .then(results => results.slice(0, -1));
}

EDIT good idea from @VLAZ to append an extra promise that forces minimal time (then slice its result off later).

The caller can say:

async function getXml(){
  // op function, no calls to "delay"
}

async function getPeople(){
  // op function, no calls to "delay"
}

// run these both together, have them take *no fewer than* 10s
padToTime([getXml(),getPeople()], 10000).then(results => {
  // we'll get here in no fewer than 10sec with the promise all results
})
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement