Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @param {() => boolean} pred
- * @param {{
- * delay?: number,
- * maxAttempts?: number
- * }} [options]
- * @returns {Promise<void>}
- */
- export const waitFor = (pred, options) => new Promise((resolve, reject) => {
- options = {
- delay: 100,
- maxAttempts: -1,
- ...options
- };
- let attempts = options.maxAttempts;
- const loop = () => {
- if (attempts-- === 0){
- reject(new Error("Max attempts exceeded."));
- }
- if (pred()){
- resolve();
- }
- setTimeout(loop, options.delay);
- }
- loop();
- });
Advertisement
Add Comment
Please, Sign In to add comment