debian-mirror-gitlab/app/assets/javascripts/lib/utils/simple_poll.js

18 lines
565 B
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import { differenceInMilliseconds } from '~/lib/utils/datetime_utility';
2019-07-07 11:18:12 +05:30
export default (fn, { interval = 2000, timeout = 60000 } = {}) => {
2017-08-17 22:00:37 +05:30
const startTime = Date.now();
return new Promise((resolve, reject) => {
2021-03-08 18:12:59 +05:30
const stop = (arg) => (arg instanceof Error ? reject(arg) : resolve(arg));
2017-08-17 22:00:37 +05:30
const next = () => {
2020-11-24 15:15:51 +05:30
if (timeout === 0 || differenceInMilliseconds(startTime) < timeout) {
2017-08-17 22:00:37 +05:30
setTimeout(fn.bind(null, next, stop), interval);
} else {
reject(new Error('SIMPLE_POLL_TIMEOUT'));
}
};
fn(next, stop);
});
};