Compare commits
No commits in common. "e75b7b8efef52fe05d613e632b81917a1ab53ad4" and "9164bae26867a35b012bf64df813786d1623a81c" have entirely different histories.
e75b7b8efe
...
9164bae268
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@tp/helpers",
|
||||
"version": "2.6.0",
|
||||
"version": "2.5.0",
|
||||
"description": "",
|
||||
"main": "closest.js",
|
||||
"scripts": {
|
||||
|
28
wait-for.js
28
wait-for.js
@ -1,28 +0,0 @@
|
||||
/**
|
||||
* Wait for some test to be true, returns a promise that resolves when condition is met.
|
||||
* Useful to wait for element render, for example. Uses requestAnimationFrame.
|
||||
*
|
||||
* @param {Function} testFn Test function to call. Should return true to resolve and false to continue testing.
|
||||
* @param {Number} maxTries Maximum number of rounds to test. If exceeded, the promise rejects. Defaults to 5000.
|
||||
* @return {Promise} Promise that resolves when test passes or rejects when maxTries is reached
|
||||
*/
|
||||
export const waitFor = (testFn, maxTries = 5000) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let tries = 0;
|
||||
const boundTestFn = testFn.bind(this);
|
||||
|
||||
function waiter() {
|
||||
if (tries === maxTries) {
|
||||
return reject(new Error('waitFor: maximum tries exceeded'));
|
||||
}
|
||||
if (!boundTestFn()) {
|
||||
window.requestAnimationFrame(waiter);
|
||||
tries++;
|
||||
return;
|
||||
}
|
||||
resolve(true);
|
||||
}
|
||||
|
||||
waiter();
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user