Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function waitFor(condition: Function, options: any): Promise {
- options = Object.assign({
- interval: 50,
- timeout: 2000
- });
- let timedOut = false
- return Promise.race([new Promise((rs, rj) => {
- window.setTimeout(() => {
- timedOut = true;
- rj('condition not met within ' + options.timeout + ' ms');
- }, options.timeout);
- }),
- new Promise((rs) => {
- const test = () => {
- try {
- if (condition()) {
- rs();
- return;
- }
- } catch (err) {}
- timedOut || window.setTimeout(test, options.interval);
- }
- test();
- })
- ]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement