2020-08-31 08:10:39 -04:00
|
|
|
import { differenceInMilliseconds } from '~/lib/utils/datetime_utility';
|
|
|
|
|
2019-03-11 16:17:43 -04:00
|
|
|
export default (fn, { interval = 2000, timeout = 60000 } = {}) => {
|
2017-05-09 00:15:34 -04:00
|
|
|
const startTime = Date.now();
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-12-23 19:10:25 -05:00
|
|
|
const stop = (arg) => (arg instanceof Error ? reject(arg) : resolve(arg));
|
2017-05-09 00:15:34 -04:00
|
|
|
const next = () => {
|
2020-08-31 08:10:39 -04:00
|
|
|
if (timeout === 0 || differenceInMilliseconds(startTime) < timeout) {
|
2017-05-09 00:15:34 -04:00
|
|
|
setTimeout(fn.bind(null, next, stop), interval);
|
|
|
|
} else {
|
|
|
|
reject(new Error('SIMPLE_POLL_TIMEOUT'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fn(next, stop);
|
|
|
|
});
|
|
|
|
};
|