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

16 lines
450 B
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
export default (fn, interval = 2000, timeout = 60000) => {
const startTime = Date.now();
return new Promise((resolve, reject) => {
2018-12-13 13:39:08 +05:30
const stop = arg => (arg instanceof Error ? reject(arg) : resolve(arg));
2017-08-17 22:00:37 +05:30
const next = () => {
if (Date.now() - startTime < timeout) {
setTimeout(fn.bind(null, next, stop), interval);
} else {
reject(new Error('SIMPLE_POLL_TIMEOUT'));
}
};
fn(next, stop);
});
};