# @baiwusanyu/utils-task

> ## Type

Latest version **1.1.2** (published 2024-12-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @baiwusanyu/utils-task
pnpm add @baiwusanyu/utils-task
yarn add @baiwusanyu/utils-task
bun add @baiwusanyu/utils-task
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2024-12-12 |
| First published | 2023-05-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | baiwusanyu-c |
| Maintainers | baiwusanyu |
| Keywords | typescript, javascript, utils, vue, vue3, vite, react |

## Links

- npm: https://www.npmjs.com/package/@baiwusanyu/utils-task
- Repository: https://github.com/baiwusanyu-c/bwsy-utils
- Homepage: https://github.com/baiwusanyu-c
- Issues: https://github.com/baiwusanyu-c/bwsy-utils/issues
- npm.io page: https://npm.io/package/@baiwusanyu/utils-task

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2024-12-12
- 1.1.1 — 2024-12-12
- 1.1.0 — 2024-12-12
- 1.0.18 — 2023-12-28
- 1.0.17 — 2023-12-28
- 1.0.16 — 2023-09-02
- 1.0.15 — 2023-08-15
- 1.0.14 — 2023-07-08
- 1.0.13 — 2023-05-25
- 1.0.12 — 2023-05-10
- 1.0.11 — 2023-05-08
- 1.0.10 — 2023-05-08
- 1.0.9 — 2023-05-07
- 1.0.8 — 2023-05-04
- 1.0.5 — 2023-05-03
- … 16 more at https://npm.io/package/@baiwusanyu/utils-task/versions

## README

# @baiwusanyu/utils-task

## Type

### ITask

任务队列实例

```typescript
export interface ITask {
    // 任务函数
    fn: (...args: any[]) => any
    // 任务函数所需参数
    params: Array<any>
    // 重试次数，默认为0
    retry: number
    // 任务唯一id
    id: number
}
```

### ITaskQueueHooks

任务队列执行钩子对象

```typescript
export interface ITaskQueueHooks {
    // 所有任务执行完毕时钩子
    onFinished?: () => void
    // 任务执行成功时钩子
    onTaskSucceeded?: (index: number, task: ITask) => void
    // 任务执行失败时钩子
    onTaskFailed?: (index: number, task: ITask) => void
}
```

## Function

### setAsyncTaskList

生产一个异步任务列表

| 参数          | 参数类型                            | 默认值     | 说明 |
|-------------|---------------------------------|---------|-----------|
| taskNum     | `Number`                        | `-` | 异步任务数 |
| taskFunc    | `(index: number) => Promise<T>` | `-` | 异步任务函数 |

| 返回值 | 返回类型             | 说明 |
|-----|------------------|---|
| taskList | `Promise<any>[]` | 任务执行列表 |

### runAsyncTaskList

执行一个异步任务列表

| 参数         | 参数类型                                                    | 默认值 | 说明        |
|------------|---------------------------------------------------------|-----|-----------|
| taskNum    | `Number`                                                | `-` | 异步任务数 |
| taskFunc   | `(index: number) => Promise<Record<any, any> / void>`   | `-` | 异步任务函数 |

| 返回值 | 返回类型             | 说明     |
|-----|------------------|--------|
| res | `Awaited<any>[]` | 执行结果列表 |

### runTaskQueue

执行一个异步任务队列，它具有重试机制，当任务执行失败（报错），回重新添加到
队列末尾等待重新执行，一直到执行成功或达到该任务的最大执行次数。

| 参数        | 参数类型              | 默认值    | 说明     |
|-----------|-------------------|--------|--------|
| taskQueue | `Array<ITask>`    | `-`    | 任务队列   |
| hook      | `ITaskQueueHooks` | `-`    | 任务队列执行钩子对象 |
| maxRetry  | `Number`          | `5`    | 最大重试数量 |
| log       | `Boolean`         | `true` | 是否打印日志 |

| 返回值 | 返回类型                            | 说明                         |
|-----|---------------------------------|----------------------------|
| res | `{success:number[], fail: number[]}` | 执行结果列表,分别对应执行成功的任务或失败任务的id |

---
_Source: https://npm.io/package/@baiwusanyu/utils-task · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
