vue-hooks-plus
Version:
Vue hooks library
39 lines (38 loc) • 1.2 kB
JavaScript
import useTimeout from "../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(() => {
onReady == null ? void 0 : onReady();
runTask();
}, delay);
}
export {
useAsyncOrder as default
};