UNPKG

vtils

Version:

一个面向业务的 JavaScript/TypeScript 实用程序库。

83 lines 2.75 kB
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose"; import _wrapNativeSuper from "@babel/runtime/helpers/esm/wrapNativeSuper"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import { sample } from 'lodash-uni'; import { wait } from "./wait.js"; export var LoopUntilRetryLimitExceededError = /*#__PURE__*/function (_Error) { _inheritsLoose(LoopUntilRetryLimitExceededError, _Error); function LoopUntilRetryLimitExceededError() { return _Error.apply(this, arguments) || this; } return LoopUntilRetryLimitExceededError; }( /*#__PURE__*/_wrapNativeSuper(Error)); /** * 循环调用某个函数直至达到某个条件后返回调用结果。 * * @param fn 要调用的函数 * @param condition 条件 * @param options 选项 */ export function loopUntil(_x, _x2, _x3) { return _loopUntil.apply(this, arguments); } function _loopUntil() { _loopUntil = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(fn, condition, options) { var retryCount, _res; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (options == null) { options = condition; condition = fn; fn = undefined; } retryCount = 0; // eslint-disable-next-line no-constant-condition case 2: if (!true) { _context.next = 22; break; } if (!fn) { _context.next = 9; break; } _context.next = 6; return fn(); case 6: _context.t0 = _context.sent; _context.next = 10; break; case 9: _context.t0 = undefined; case 10: _res = _context.t0; _context.next = 13; return condition(_res); case 13: if (!_context.sent) { _context.next = 15; break; } return _context.abrupt("return", _res); case 15: if (!(options.retryLimit && retryCount >= options.retryLimit)) { _context.next = 17; break; } throw new LoopUntilRetryLimitExceededError('已达到最大重试次数'); case 17: retryCount++; _context.next = 20; return wait(typeof options.retryDelay === 'number' ? options.retryDelay : sample(options.retryDelay)); case 20: _context.next = 2; break; case 22: case "end": return _context.stop(); } }, _callee); })); return _loopUntil.apply(this, arguments); }