ins-async-tasks
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

npm Coverage Status npm npm bundle size

ins-async-task

ins-async-task is a lightweight npm library that simplifies handling asynchronous tasks in JavaScript and TypeScript. It provides a set of utility functions to manage asynchronous operations effectively.

Table of Contents

Install

    npm install ins-async-tasks --save

Usage

   import * as AsyncTasks from 'ins-async-tasks';
   //or
   const AsyncTasks=require('ins-async-tasks');

API

data structure information

define settledValue,task,tasks

    const task=()=>new Promise((resolve,reject)=>{})
    const tasks=[task]
    const settledValue=[
        {
            status: 'fulfilled',
            value:"your resolved data can be string or any object",
        },
          {
            status: 'rejected',
            reason:new Error('error detail message')
        },
        {
            status: 'fulfilled',
            value:"your resolved data can be string or any object",
        }
    // ]

waterfall()

Different types of tasks will be executed in ways of serial

Effect:
  Task1 ----->|
  Task2    -------->|
  Task3         --->|

    const res=await AsyncTasks.waterfall([task1,task2,task3])
    // res is settledValue;

all()

Different types of tasks will be executed in ways of parallel and ignore error

Effect:
  Task1 ----->|
  Task2 -------->|
  Task3 --->|

    const res=await AsyncTasks.all([task1,task2,task3])
    // if task2 has error, res=["data1","data3"]

chunkTask()

Large numbers of Tasks chunked with group and Executed,the tasks in group are executed in parallel,different groups are in serial. error will be ignored.

Effect:
  Group1
    Task1 ----->|
    Task2 -------->|
    Task3 --->|
  Wait         |-->|
  Group2
    Task4        ----->|
    Task5        -------->|
    Task6        ----------->|
  Wait                   |-->|
    Task7                 ---------->|

    //group chunked with 3 tasks,and sleep for 1000 milliseconds between different groups;

    const res=await AsyncTasks.chunkTask(tasks,3,1000)
    // res=[
    //     [ "data1","data2","data3"],
    //     ["data4","data5","data6"],
    //     [,"data7"],
    // ]
    const flatResult=res.flat();
    // flatResult=["data1","data2","data3","data4","data5","data6","data7"]

waterfallList()

Compose and execute tasks from array in ways of serial

    const list=[1,2,3]
    const res=await AsyncTasks.waterfallList(list,(item,i,resolve,reject)=>{
        if(i!=1){
            setTimeout(()=>{
                resolve(`data${i+1}`)
            },i*1000)
        }else{
            reject(new Error(`task with index of ${i} failed`)
        }
    })
    // res is settledValue;

allList()

Compose and execute tasks from array in ways of parallel and ignore error

    const list=[1,2,3]
    const res=await AsyncTasks.allList(list,(item,i,resolve,reject)=>{
        if(i!=1){
            setTimeout(()=>{
                resolve(`data${i+1}`)
            },i*1000)
        }else{
            reject(new Error(`task with index of ${i} failed`)
        }
    })
    // res=['data1','data3']

allSettleList()

Different types of tasks will be executed in ways of parallel the same effect with AsyncTasks.allList but return settled value

    const list=[1,2,3]
    const res=await AsyncTasks.allSettleList(list,(item,i,resolve,reject)=>{
        if(i!=1){
            setTimeout(()=>{
                resolve(`data${i+1}`)
            },i*1000)
        }else{
            reject(new Error(`task with index of ${i} failed`)
        }
    })
    // res is settledValue;

allSettle()

Different types of tasks will be executed in ways of parallel the same effect with AsyncTasks.all but return settled value

    const res=await AsyncTasks.all([task1,task2,task3])
    // res is settledValue;

chunkSettle()

the same effect with chunkTask but catch all error and reject;

    //group chunked with 3 tasks,and sleep for 1000 milliseconds between different groups;
    const res=await AsyncTasks.chunkSettle(tasks,3,1000)
    const flatResult=res.flat();
    // flatResult is settledValue

/ins-async-tasks/

    Package Sidebar

    Install

    npm i ins-async-tasks

    Weekly Downloads

    6

    Version

    1.0.9

    License

    ISC

    Unpacked Size

    19.3 kB

    Total Files

    8

    Last publish

    Collaborators

    • insonghua2