File size: 553 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
const noop = () => {};
const WAIT_INITIAL = 1; // initial wait in milliseconds
const WAIT_MULTIPLIER = 2;
const WAIT_MAX = 2048; // give up waiting when delay has grown to ~4 seconds

const wait = ( { condition, consequence, delay = 0, onError = noop } ) => {
	if ( condition() ) {
		consequence();
		return;
	}

	if ( delay >= WAIT_MAX ) {
		onError();
		return;
	}

	window.setTimeout(
		wait.bind( null, {
			condition,
			consequence,
			delay: delay ? delay * WAIT_MULTIPLIER : WAIT_INITIAL,
			onError,
		} ),
		delay
	);
};

export default wait;