12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- "use strict";
- const log = require("./logger");
- function delay(time) {
- return new Promise((resolve) => {
- log.info("Delaying for %d ms", time);
- setTimeout(() => resolve(time), time);
- });
- }
- function retryPromiseUntil(callback, checkFn, maxAttempts) {
- if (!callback.hasOwnProperty("attemptCount")) {
- callback.attemptCount = 0;
- }
- let result = checkFn();
- log.debug("Retrying promise...");
- if (result) {
- log.info("Check function returned", result);
- return result;
- }
- callback.attemptCount++;
- log.debug("Performing attempt", callback.attemptCount);
- if (maxAttempts && callback.attemptCount > maxAttempts) {
- log.warn("Max attempts reached exiting");
- return;
- }
-
- return Promise.resolve()
- .then(() => callback())
- .then(() => retryPromiseUntil(callback, checkFn, maxAttempts));
- }
- function promiseChain(promiseFunctions) {
- log.debug("Chaining %d promises", promiseFunctions.length);
- return promiseFunctions.reduce((prev, current, index) => {
- log.debug("Chaining promise #%d", index);
-
- return prev.then(() => current());
- }, Promise.resolve());
- }
- module.exports = {
- delay: delay,
- retryPromiseUntil: retryPromiseUntil,
- promiseChain: promiseChain
- };
|