Exponential BackOff

an anonymous user · September 23, 2020 · NodeJS
function calculateBackOff(x) {
  const result = Math.pow(1.25, x - 3);
  console.log("the result was",result);
  return result;
}
function exponentialBackOff(_try = 1, accumulation = 0) {
  if (accumulation >= 1000) {
    return console.log("END!\nThe total tume was", accumulation);
  }
  _try += 1;
  const backOffTime = calculateBackOff(_try);
  accumulation += backOffTime;
  setTimeout(() => {
    console.log("Try number", _try);
    console.log("Total accumulated time", accumulation);
    exponentialBackOff(_try, accumulation);
  }, backOffTime);
}
exponentialBackOff();

Comments

Please sign up or log in to contribute to the discussion.