Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. function waitFor(condition: Function, options: any): Promise {
  2. options = Object.assign({
  3. interval: 50,
  4. timeout: 2000
  5. });
  6.  
  7. let timedOut = false
  8. return Promise.race([new Promise((rs, rj) => {
  9. window.setTimeout(() => {
  10. timedOut = true;
  11. rj('condition not met within ' + options.timeout + ' ms');
  12. }, options.timeout);
  13. }),
  14. new Promise((rs) => {
  15. const test = () => {
  16. try {
  17. if (condition()) {
  18. rs();
  19. return;
  20. }
  21. } catch (err) {}
  22. timedOut || window.setTimeout(test, options.interval);
  23. }
  24. test();
  25. })
  26. ]);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement