Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Repeats a Promise after it resolves every x ms until the predicate function returns true.
- *
- * @param {Promise} repeatPromise The Promise to repeat.
- * @param {Function} predicateFunction The function that accepts the results
- * of the promise and returns true to stop repeating.
- * @param {Number} time The time to wait before repeating the promise in ms.
- *
- * @return {Promise}
- */
- const repeatUntil = (repeatPromise, predicateFunction, time) =>
- new Promise((resolve, reject) => {
- const repeat = () =>
- repeatPromise()
- .then((response) => {
- if (predicateFunction(response)) {
- resolve(response);
- } else {
- setTimeout(repeat, time);
- }
- })
- .catch(reject);
- });
- export default repeatUntil;
Add Comment
Please, Sign In to add comment