UNPKG

@house-agency/brewtils

Version:

The Brewery Node.js Utilities (brewtils)

50 lines (38 loc) 998 B
const format = require('util').format; const log = require('./log'); const q = require('q'); const defaults = { maximum_retries: 10, delay: 100 }; function retry(func, maximum_retries, delay, filter_error_cb) { if (!maximum_retries) { maximum_retries = defaults.maximum_retries; } if (!delay) { delay = defaults.delay; } const retry_func = (retry_count) => func() .catch(error => { if(filter_error_cb && !filter_error_cb(error)) throw error; log( 'error', 'Error when invoking', func.name, format('Retrying %d of %d', retry_count, maximum_retries), error ); if (retry_count < maximum_retries) { return q.delay(delay) .then(() => retry_func(retry_count + 1)); } throw error; }); return retry_func(1); } const wrapper = Object.assign( retry, {} ); module.exports = wrapper;