blob: 0bf3e558342fd60e1bbd2cf85b3bcf7c5db6f4e1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function waitFor(condition, MAX_FRAME = 500) {
return new Promise((resolve, reject) => {
function tick(frames) {
// We requestAnimationFrame either for MAX_FRAME frames or until condition is
// met.
if (frames >= MAX_FRAME)
reject(new Error(`Condition did not become true after ${MAX_FRAME} frames`));
else if (condition())
resolve();
else
requestAnimationFrame(() => tick(frames + 1));
}
tick(0);
});
}
|