UNPKG

tryloop

Version:

Simple library for retrying operations, it supports multiple backoff strategies.

22 lines (21 loc) 618 B
/* IMPORT */ import Abstract from './abstract.js'; /* MAIN */ class Exponential extends Abstract { /* CONSTRUCTOR */ constructor(options) { super(options); this.options = Object.assign({ factor: 2, minInterval: 10, maxInterval: 180000 }, this.options); } /* API */ schedule(fn) { const interval = Math.max(this.options.minInterval, Math.min(this.options.maxInterval, this.options.minInterval * Math.pow(this.options.factor, this.tries))); setTimeout(() => fn(), interval); } } /* EXPORT */ export default Exponential;