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 | 简体中文
The async-task-mapping library does not use any third-party dependency packages and does not limit the technology stack.
npm i async-task-mapping --save
import { createTaskList } from 'async-task-mapping';
const asyncTask = createTaskList({ordered: true, requestCount: 2, responseCount: 2})
setTimeout(async () => {
// A Promise.resolve is returned here. You can use either async/await or .then.
const data = await asyncTask.request()
console.log('data-1', data)
}, 100)
setTimeout(async () => {
const data = await asyncTask.request(() => console.log('request-2'))
console.log('data-2', data)
}, 200)
setTimeout(() => {
asyncTask.pushResponse('response1')
}, 300)
setTimeout(() => {
asyncTask.pushResponse('response2')
}, 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
name | description | default |
---|---|---|
ordered | Is it orderly | false |
requestCount | Number of times to access data | 1 |
responseCount | Increase the number of times of data | 1 |
name | description | returned data structure |
---|---|---|
request | Used to access data | Promise type, The data obtained is the original data of pushResponse. If the responseCount is greater than 1, an array will be obtained |
pushResponse | Used to add data | -- |
clear | Clear all statuses | -- |
getStatus | Get current status | { // Whether to complete all request binding requestDone: false, // Whether to complete all pushResponse responseDone: false, //Number of requests bound requestCount: 1 (Number of requests bound), // Number of pushResponse complete responseCount: 1 (Number of pushResponse completed) } |
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.request()
console.log('data1', data)
}, 230)
setTimeout(async () => {
const data = await taskOrder.request(() => console.log('request 2'))
console.log('data2', data)
}, 500)
setTimeout( () => {
taskOrder.pushResponse('Response1')
}, 200)
setTimeout(() => {
taskOrder.pushResponse('Response2')
}, 300)
setTimeout(() => {
taskOrder.pushResponse('Response3')
}, 400)
You can use these functions in different methods. The result of the above code is:
// time 230: data1 Response1
// time 500: data2 Response2
name | description | returned data structure |
---|---|---|
request | The method to access data, which can be passed in a function to be executed immediately |
Promise type, the data obtained is the original data of pushResponse |
pushResponse | Used to add data | -- |
clear | Clear all statuses | -- |
getStatus | Get current status | { //Number of incomplete matching requests pendingRequests: 0, //Number of responses that have not completed matching pendingResponses: 0 } |
getLastCompletedTask | Get the data matching the last request with the response | -- |
MIT.