1.0.9 • Published 3 months ago

ins-async-tasks v1.0.9

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

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
```typescript
    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
1.0.9

3 months ago

1.0.8

3 months ago

1.0.7

3 months ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago