CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
retryable.js77 linesDownload Raw Back to async
1'use strict';2 3Object.defineProperty(exports, "__esModule", {4    value: true5});6exports.default = retryable;7 8var _retry = require('./retry.js');9 10var _retry2 = _interopRequireDefault(_retry);11 12var _initialParams = require('./internal/initialParams.js');13 14var _initialParams2 = _interopRequireDefault(_initialParams);15 16var _wrapAsync = require('./internal/wrapAsync.js');17 18var _wrapAsync2 = _interopRequireDefault(_wrapAsync);19 20var _promiseCallback = require('./internal/promiseCallback.js');21 22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }23 24/**25 * A close relative of [`retry`]{@link module:ControlFlow.retry}.  This method26 * wraps a task and makes it retryable, rather than immediately calling it27 * with retries.28 *29 * @name retryable30 * @static31 * @memberOf module:ControlFlow32 * @method33 * @see [async.retry]{@link module:ControlFlow.retry}34 * @category Control Flow35 * @param {Object|number} [opts = {times: 5, interval: 0}| 5] - optional36 * options, exactly the same as from `retry`, except for a `opts.arity` that37 * is the arity of the `task` function, defaulting to `task.length`38 * @param {AsyncFunction} task - the asynchronous function to wrap.39 * This function will be passed any arguments passed to the returned wrapper.40 * Invoked with (...args, callback).41 * @returns {AsyncFunction} The wrapped function, which when invoked, will42 * retry on an error, based on the parameters specified in `opts`.43 * This function will accept the same parameters as `task`.44 * @example45 *46 * async.auto({47 *     dep1: async.retryable(3, getFromFlakyService),48 *     process: ["dep1", async.retryable(3, function (results, cb) {49 *         maybeProcessData(results.dep1, cb);50 *     })]51 * }, callback);52 */53function retryable(opts, task) {54    if (!task) {55        task = opts;56        opts = null;57    }58    let arity = opts && opts.arity || task.length;59    if ((0, _wrapAsync.isAsync)(task)) {60        arity += 1;61    }62    var _task = (0, _wrapAsync2.default)(task);63    return (0, _initialParams2.default)((args, callback) => {64        if (args.length < arity - 1 || callback == null) {65            args.push(callback);66            callback = (0, _promiseCallback.promiseCallback)();67        }68        function taskFn(cb) {69            _task(...args, cb);70        }71 72        if (opts) (0, _retry2.default)(opts, taskFn, callback);else (0, _retry2.default)(taskFn, callback);73 74        return callback[_promiseCallback.PROMISE_SYMBOL];75    });76}77module.exports = exports.default;
basant307/AI_Governance_Project · CoolFace