async-multiple
A lite asynchronous queue runtime library!
Installing
Using npm:
$ npm install async-multiple
Example
map
const task = 1 2 3 4 5 // result is order: 0 input: 1 output: 1 error: null order: 1 input: 2 output: 4 error: null order: 2 input: 3 output: null error: 'not 3!' // error object order: 3 input: 4 output: 16 error: null order: 4 input: 5 output: 25 error: null
each
// result is order: 0 output: 1 error: null order: 1 output: 2 error: null order: 2 output: 3 error: null order: 3 output: null error: 'not 4' order: 5 output: 5 error: null
API
map(config)
loop task can be made by passing the relevant config to map
.
task
required: true type: array
example
handle
required: true type: function
callback return: promise
NOTE
When handle wasn't return a promise but a value like number or string, it will be wrap by Promise.resolve. If you have no return, the task will stop at this step!
callback params: (input, step, cancelTask)
input: data from task array
step: task called step
cancalTask: global cancel function, if called, task will stop at this step, that means you can cancel task at before every step
example
// return a value // same as // handle error // use async/await
rejectOnError
required: false type: boolean default: false
set true: when receive a reject, task will stop at this step
example
// result order: 0 input: 1 output: 1 error: null order: 1 input: 2 output: 4 error: null order: 2 input: 3 output: null error: 'not 3!' // error object
randomStep
required: false type: boolean default: false
set true: random call task
example
// resultbegin handle at step 0 input is 2 begin handle at step 1 input is 5 begin handle at step 2 input is 1 begin handle at step 3 input is 3 begin handle at step 4 input is 4 order: 0 input: 1 output: 1 error: null order: 1 input: 2 output: 2 error: null order: 2 input: 3 output: 3 error: null order: 3 input: 4 output: 4 error: null order: 4 input: 5 output: 5 error: null
stepBettwen
required: false type: [number, array] default: 0
example
// every task will sleep 1000ms, then called // every task will sleep 500~2000ms, then called
stepTimeout
required: false type: number default: undefined
example
// if step handle time longer then stepTimeout, this step will reject, then call next step// and receive a timeout error
errorRetry
required: false type: number default: 0
example
// if set, when receive a reject, handle will recall at next step// if called times more than errorRetry, get error result, then call next stepconst task = 1 2 3 4 5let flag = false
maxSuccessCount
required: false type: number default: undefined
example
// if set, when receive enough resolve, task will cancel then stop at next step // resultbegin handle at step 0 input is 1 begin handle at step 1 input is 2 begin handle at step 2 input is 3 async-multiple MESSAGE: Enough success result task will stop! order: 0 input: 1 output: 1 error: null order: 1 input: 2 output: 2 error: null order: 2 input: 3 output: 3 error: null
showProgress
required: false type: boolean default: false
example
// if set, when receive enough resolve, task will cancel then stop at next stepconst task = 1 2 3 4 5 // result TASK_csv1LTmw all: 5 complete: 1 progress: 2000% TASK_csv1LTmw all: 5 complete: 2 progress: 4000% TASK_csv1LTmw all: 5 complete: 3 progress: 6000% TASK_csv1LTmw all: 5 complete: 4 progress: 8000% TASK_csv1LTmw all: 5 complete: 5 progress: 10000% order: 0 input: 1 output: 1 error: null order: 1 input: 2 output: 2 error: null order: 2 input: 3 output: 3 error: null order: 3 input: 4 output: 4 error: null order: 4 input: 5 output: 5 error: null
alias
required: false type: string default: undefined
example
// if set, when receive enough resolve, task will cancel then stop at next stepconst task = 1 2 3 4 5 // result loop task all: 5 complete: 1 progress: 2000% loop task all: 5 complete: 2 progress: 4000% loop task all: 5 complete: 3 progress: 6000% loop task all: 5 complete: 4 progress: 8000% loop task all: 5 complete: 5 progress: 10000% order: 0 input: 1 output: 1 error: null order: 1 input: 2 output: 2 error: null order: 2 input: 3 output: 3 error: null order: 3 input: 4 output: 4 error: null order: 4 input: 5 output: 5 error: null
handleStart | handleEnd
required: false type: function default: undefined
example
const task = 1 2 3 4 5 // resultbegin handle at step 0 input is 1 end handle at step 0 input is 1 begin handle at step 1 input is 2 end handle at step 1 input is 2 begin handle at step 2 input is 3 end handle at step 2 input is 3 begin handle at step 3 input is 4 end handle at step 3 input is 4 begin handle at step 4 input is 5 end handle at step 4 input is 5 order: 0 input: 1 output: 1 error: null order: 1 input: 2 output: 2 error: null order: 2 input: 3 output: 3 error: null order: 3 input: 4 output: 4 error: null order: 4 input: 5 output: 5 error: null
each(config)
All params is same as map, except task && handle(each havn't this params)
task
required: true type: array
diff each's mask receive a list contain handle you want to call
example
const task = {} // what to do 1 {} // what to do 2 {} // what to do 3 {} // what to do 4
CONSTRUCT
Map | Each
example
const loop = task: 1 2 3 4 5 sloopstart const loop = task: {} // what to do 1 {} // what to do 2 {} // what to do 3 {} // what to do 4 loopstart
method
start
call this method to start the task, return a promise
cancelTask
call this method to cancel the task global
Event
example
const eventBus = debug: true eventBus eventBus
method
on
params
actionName: the action you listening callback: callback method, params is received data callTime: max times that callback run, if undefined, no times limit
NOTE
You can register it many times, like that:
const eventBus = debug: true eventBus eventBus
Every callback will be called
once
params
actionName: the action you listening callback: callback method, params is received data, can called once
emit
params
actionName: the action you emit data: the data you send
Todo
call mutli task at the same time
License (MIT)
Copyright (c) 2019 Leo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.