Skip to content

Instantly share code, notes, and snippets.

@itsdouges
Last active October 27, 2016 08:37
Show Gist options
  • Save itsdouges/21211a580d4b2ad93de305c5d1c144d9 to your computer and use it in GitHub Desktop.
Save itsdouges/21211a580d4b2ad93de305c5d1c144d9 to your computer and use it in GitHub Desktop.
Promise Retries
const _ = require('lodash');
module.exports = ({ retryPredicate = _.noop, retryCount = 5 } = {}) => (promiseFunc) => {
let retries = retryCount;
return function retry (...args) {
return promiseFunc(...args).catch((e) => {
if (retryPredicate(e) && retries > 1) {
retries -= 1;
return retry(...args);
}
return Promise.reject(e);
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment