0.0.1 • Published 2 years ago

mul-promise v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

mul-promise

simple package allow run promises in mul-flow fashion

https://tachibana-shin.github.io/mul-promise

Build NPM Size Languages License Star Download

Installation

NPM / Yarn / Pnpm

pnpm add mul-promise

CDN:

<script src="https://unpkg.com/"></script>

Usage

import multiPromise from "mul-promise"


// eslint-disable-next-line no-redeclare
function multiPromise<DataTasks extends unknown[], ResultTask = unknown>(
  dataForTasks: DataTasks, // data for create tasks
  createTask: <Index extends number>(
    data: DataTasks[Index],
    index: Index
  ) => Promise<ResultTask>, // function create task
  concurrent = 5, // count task concurrent run
  onTaskDone?: (resultTask: ResultTask, index: number) => Promise<ResultTask>, // on one task done
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  onTaskError?: (error: any, index: number) => Promise<ResultTask>, // on one task error
  skipError?: boolean // no stop tasks on error
): Promise<(ResultTask | null)[]>

Example

import multiPromise from "mul-promise"

await multiPromise(
  ["Hello World", "Koniichiwa", "Ohayo", "Yahalo", "Kobanwa"],
  (message) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(message)
      }, 1000)
    })
  }
)