All files / util promise-util.js

45.45% Statements 5/11
0% Branches 0/2
40% Functions 2/5
50% Lines 4/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 182x   2x 2x                           2x
const wait = ms => new Promise(r => setTimeout(r, ms));
 
const retryPromise = (operation, delay, times) => new Promise((resolve, reject) => {
    return operation()
        .then(resolve)
        .catch((reason) => {
            if (times - 1 > 0) {
                console.log("Retry to connect to mongodb...")
                return wait(delay)
                    .then(retryPromise.bind(null, operation, delay, times - 1))
                    .then(resolve)
                    .catch(reject);
            }
            return reject(reason);
        });
});
 
module.exports = { retryPromise };