aboutsummaryrefslogtreecommitdiff
path: root/e2e-lanthan/eventually.js
blob: c04c2779c91037e2758af7aba2abf4feb96469f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const assert = require('assert');

let defaultInterval = 100;
let defaultTimeout = 2000;

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
 
const eventually = async (fn, timeout = defaultTimeout, interval = defaultInterval) => {
  let start = Date.now();
  let loop = async() => {
    try {
      await fn();
    } catch (err) {
      if (Date.now() - start > timeout) {
        throw err;
      }
      await new Promise((resolve) => setTimeout(resolve, interval))
      await loop();
    }
  };
  await loop();
};
module.exports = eventually;