Welcome to mirror list, hosted at ThFree Co, Russian Federation.

simple_poll.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e4e9fb2e6fa39d4a6b8197e5c2af166d185ba479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { differenceInMilliseconds } from '~/lib/utils/datetime_utility';

export default (fn, { interval = 2000, timeout = 60000 } = {}) => {
  const startTime = Date.now();

  return new Promise((resolve, reject) => {
    const stop = arg => (arg instanceof Error ? reject(arg) : resolve(arg));
    const next = () => {
      if (timeout === 0 || differenceInMilliseconds(startTime) < timeout) {
        setTimeout(fn.bind(null, next, stop), interval);
      } else {
        reject(new Error('SIMPLE_POLL_TIMEOUT'));
      }
    };
    fn(next, stop);
  });
};