Guest User

Untitled

a guest
Oct 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. /**
  2. * Repeats a Promise after it resolves every x ms until the predicate function returns true.
  3. *
  4. * @param {Promise} repeatPromise The Promise to repeat.
  5. * @param {Function} predicateFunction The function that accepts the results
  6. * of the promise and returns true to stop repeating.
  7. * @param {Number} time The time to wait before repeating the promise in ms.
  8. *
  9. * @return {Promise}
  10. */
  11. const repeatUntil = (repeatPromise, predicateFunction, time) =>
  12. new Promise((resolve, reject) => {
  13. const repeat = () =>
  14. repeatPromise()
  15. .then((response) => {
  16. if (predicateFunction(response)) {
  17. resolve(response);
  18. } else {
  19. setTimeout(repeat, time);
  20. }
  21. })
  22. .catch(reject);
  23. });
  24.  
  25. export default repeatUntil;
Add Comment
Please, Sign In to add comment