1.0.5 • Published 6 months ago

async-task-mapping v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

async-task-mapping

It can make asynchronous tasks that promise cannot complete, ignore the order of sending and receiving, allow binding of asynchronous tasks in pending status, allow triggering a task in multiple functions, and support producer consumer mode and order mode.

English | 简体中文

Pre-requisites

The async-task-mapping library does not use any third-party dependency packages and does not limit the technology stack.

Install

npm i async-task-mapping --save

Usage

use createTaskList

version

import { createTaskList } from "async-task-mapping";
const asyncTask = createTaskList({
  ordered: true,
  taskCount: 2,
  resolveCount: 2,
});

setTimeout(async () => {
  // A Promise.resolve is returned here. You can use either async/await or .then.
  const { list, dataMap } = await asyncTask;
  console.log("data-1", list[0]);
  // or
  asyncTask.then(({ list, dataMap }) => {
    console.log("data-1", list[0]);
  });
}, 100);

setTimeout(async () => {
  const { list, dataMap } = await asyncTask;
  console.log("data-2", dataMap.res2);
}, 200);

setTimeout(() => {
  asyncTask.pushResolve("response1");
}, 300);

setTimeout(() => {
  asyncTask.pushResolve("response2", "res2");
}, 400);

You can use these functions in different methods. The result of the above code is:

// time 200: request-2
// time 300: data-1 response1
// time 400: data-2 response2

Parameters passed in during createTaskList instantiation

namedescriptiondefault
orderedIs it orderlyfalse
taskCountNumber of times to access data1
resolveCountIncrease the number of times of data1

Methods on createTaskList Instances

namedescriptionreturned data structure
then*The original method of promisetask.then(({list, dataMap})=>{})
pushResolveUsed to add datapushResolve(data, name), Not required, data of Promise.resolve
clearClear all statuses--
pausedpause task--
runningcontinue matching tasks--
getStatusGet current status{  // Whether to complete all request binding    requestDone: false,   // Whether to complete all pushResolve   responseDone: false,    //Number of requests bound    requestCount: 1 (Number of requests bound),    // Number of pushResolve complete  responseCount: 1 (Number of pushResolve completed)}

use createTaskOrder

The request and response can be out of order.

import { createTaskOrder } from "async-task-mapping";
const taskOrder = createTaskOrder();

setTimeout(async () => {
  // A Promise.resolve is returned here. You can use either async/await or .then.
  const data = await taskOrder;
  console.log("data1", data);
}, 230);

setTimeout(async () => {
  const data = await taskOrder;
  console.log("data2", data);
}, 500);
setTimeout(() => {
  taskOrder.pushResolve("resolve1");
}, 200);
setTimeout(() => {
  taskOrder.pushResolve("resolve2");
}, 300);
setTimeout(() => {
  taskOrder.pushResolve("resolve3");
}, 400);

You can use these functions in different methods. The result of the above code is:

// time 230: data1 resolve1
// time 500: data2 resolve2

Methods on createTaskOrder Instances

namedescriptionreturned data structure
pushResolveUsed to add data--
clearClear all statuses--
pausedpause task--
runningcontinue matching tasks--
getStatusGet current status{  //Number of incomplete matching requests  pendingRequests: 0,   //Number of responses that have not completed matching  pendingResponses: 0 }
getLastCompletedTaskGet the data matching the last request with the response--

License

MIT.

1.0.2

7 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.1

1 year ago

1.0.0

1 year ago

1.0.0-beta

1 year ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago