UNPKG

asyncc

Version:
60 lines (59 loc) 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = times; var _utils = require("./_utils"); /** * Run `task` repeatedly until number `times` is reached. * * Stops at the first error encountered. * An optional `lag` between retries may be used. * * @name times * @memberOf module:serial * @static * @method * @param {Number|Object} times - runs `times` times. If `times < 0` then "times" cycles endlessly until an error occurs. * @param {Number} [times.times=0] - max. number of retries * @param {Number} [times.lag=0] - time-lag in ms between retries * @param {Function} task - iterator function of type `function (cb: Function, index: Number)` * @param {Function} [callback] - optional callback `function (errors: Error, result: Array<any>)` * * @example * let arr = [] * times({times: 4, lag: 100}, // 4 times with 100ms time-lag between retries * (cb, index) => { * arr.push(index) * cb(null, index) * }, (err, res) => { * //> err = null * //> res = 3 * //> arr = [0, 1, 2, 3] * } * ) */ function times(num, task, callback) { var i = 0; var _times2 = (0, _utils._times)(num), times = _times2.times, lag = _times2.lag, fn = _times2.fn; if (times) { run(); } else { callback && callback(); } function cb(err, res) { if (err || times > 0 && i >= times) { callback && callback(err, res); } else { fn(function () { run(); }, lag); } } function run() { task(cb, i++); } }