gitlab-org--gitlab-foss/app/assets/javascripts/lib/utils/simple_poll.js

18 lines
565 B
JavaScript
Raw Normal View History

import { differenceInMilliseconds } from '~/lib/utils/datetime_utility';
export default (fn, { interval = 2000, timeout = 60000 } = {}) => {
2017-05-09 00:15:34 -04:00
const startTime = Date.now();
return new Promise((resolve, reject) => {
const stop = (arg) => (arg instanceof Error ? reject(arg) : resolve(arg));
2017-05-09 00:15:34 -04:00
const next = () => {
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);
});
};