Promise Queue +
Promise-based queue. Support timeout, retry and so on.
demo
var Queue = ;var q = QueuePromise; //a Promise utils;//Realize a queue with a maximum concurrency of 1var queue1 =;//a return promise function{return {}}var { console; }queue1 //add job (FIFO);queue1 //The normal function returns a promise according to the Promise / A + rule;queue1 //add job (LIFO);queue1 //Add multiple jobs with Array, Work done will execute 'workResolve'queue1 //Add multiple jobs with Map,queue1//queue1.start(); //queue start;queue1/*Equivalent to:queue1.push(testfn,['go']).then(console.log);queue1.start();* In general, it is convenient to use the 'go'*/// Output:/*01234[ 3, 4 ]567{ a: 5, b: 6, c: 7 }8go*/
API
new Queue(maxConcurrent,options)
Creates a queue;
maxConcurrent
MaxNumber Concurrent
about options like:
var Queue = ;var queue =
Queue.Q / Queue.Promise
a Promise utils, See also extend-promise#类扩展
Note: The prototype is not expanded with extend-promise;
Queue.use(Promise) , Queue.createUse(Promise)
Modify the internal use of Promise , the default use of bluebird
Queue.use(Promise)
usePromise
;Queue.createUse(Promise)
return new Class usePromise
;
var Queue = //default use of bluebirdQueuePromisedeferpromise instanceof Promise; //false//use ES6 PromiseQueue; QueuePromisedeferpromise instanceof Promise; //truevar queue1 = ;queue1 instanceof Promise; //true; //Create a new class `NQueue`, does not affect the original` Queue`;var NQueue = Queue; //use q module //use create function (v1.2+)var Queue = Promise;
queue.push(promisefun, args[], options)
add job (FIFO)
promisefun
promise functionargs
arguments about options like:
queue;
queue.unshift(promisefun, args[], options)
add job (LIFO)
queue.go(promisefun, args[], options)
like push
and start queue
queue.jump(promisefun, args[], options)
like unshift
and start queue
queue.add(executor, options, start, jump)
executor
like thenew Promise(executor)
options
like 'options' forpush
start
Whether to start immediatelyjump
Whether the LIFO mode
queue//output 1;
queue.addArray(arr,start,jump)
Add multiple jobs with Array, promise value as Array;
arr
arguments Arraystart
Whether to start immediatelyjump
Whether the LIFO mode
queue;//output [1,3]
queue.addProps(props,start,jump)
Add multiple jobs with Array, promise value as Map;
props
arguments Mapstart
Whether to start immediatelyjump
Whether the LIFO mode
queue;//output {a:1,b:3}
queue.addLikeArray(arrArgs[],promisefun,options,start,jump)
Syntax for 'addArray' sugar
{ var i = 0sum; whilei<argumentslength sum = i == 0 ? argumentsi : sum + argumentsi; return sum;}queue;//output [1,3]
queue.addLikeArrayEach (arrArgs[],promisefun,options,start,jump)
like queue.addLikeArray
,To "promisefun" pass parameters similar to "forEach" (element, index, arrArgs)
{ return i + " : " + v;}queue;//output [ '0 : 1', '1 : 1,2' ]
queue.addLikeProps (props,promisefun,options,start,jump)
Syntax for 'addProps' sugar
queue.addLikePropsEach (props,promisefun,options,start,jump)
like queue.addLikeProps
,To "promisefun" pass parameters similar to "forEach" (value, key, props)
queue.start()
start queue;
queue.stop()
stop queue;
queue.clear(reason)
clear queue,The rest of the queue will be rejected with reason
;
queue.option(name, value)
Set/Get queue options
queue.getMax()
Get MaxNumber Concurrent;
queue.setMax(newMax)
Set MaxNumber Concurrent;
queue.getLength()
Queue remaining job count;
queue.getRunCount()
Queue running job count;
queue.isStart()
queue.onError = function(err){}
Queue other error handling
About the browser
Because bluebird
too big, Of browsers use easy-promise instead of bluebird;
- dist/promise-queue-plus.js
- dist/promise-queue-plus.min.js (gzip 3.8k)