UNPKG

vue-hooks-plus

Version:
40 lines (39 loc) 1.41 kB
"use strict"; const useTimeout = require("../useTimeout"); const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e }; const useTimeout__default = /* @__PURE__ */ _interopDefaultLegacy(useTimeout); function useAsyncOrder({ task, option }) { const { delay = 0, onError, onReady, onSuccess } = option != null ? option : {}; if (!(task instanceof Array)) { throw new Error("task must be Array"); } const interruptibleError = (reason) => { onError == null ? void 0 : onError(reason); }; const interruptibleReject = (resolve) => { return (error) => { interruptibleError(error); resolve == null ? void 0 : resolve({ error }); }; }; const runTask = () => { var _a; (_a = Array(...task.keys())) == null ? void 0 : _a.reduce((promise, index) => { const promise_ = promise.then((res) => { if (!(res == null ? void 0 : res.error)) { onSuccess == null ? void 0 : onSuccess(res); } return new Promise((resolve) => { var _a2; (_a2 = task == null ? void 0 : task[index]) == null ? void 0 : _a2.call(task, resolve, interruptibleReject(resolve), index); }); }); return promise_; }, Promise.resolve()); }; useTimeout__default.default(() => { onReady == null ? void 0 : onReady(); runTask(); }, delay); } module.exports = useAsyncOrder;