new Queue(task, concurrency)
Run queued items
through an asynchronous task
.
Once finishing the task
an optional callback is called.
While pushing to the queue, you may define a priority for execution.
Lower values means faster execution.
Parameters:
Name | Type | Description |
---|---|---|
task |
function | iterator function of type |
concurrency |
Number | max. number of tasks running in parallel |
Examples
var arr = []
var q = new Queue((item, cb) => {
arr.push(item)
cb(null, item)
})
// push item "one" at end of queue
q.push('one', (err, res) => {
console.log(res + ' finished')
})
// add item "two" at start of queue
q.unshift('two', () => {
console.log('two finished')
})
// called when all items in queue where processed
q.drain(() => {
console.log(arr)
//> arr = ['one', 'two']
})
let arr = []
let q = new Queue(function (item, cb) {
arr.push(item)
cb()
}, 2)
q.concat([100, 101, 102], 3) // priority = 3 - last (but 2 items already processed)
q.concat([0, 1, 2], 1) // priority = 1 - first
q.concat([10, 11, 12], 2) // priority = 2 - second
q.drain(() => {
//> arr = [ 100, 101, 0, 1, 2, 10, 11, 12, 102 ])
})
Members
idle
Check if queue is idle - means no items in queue and no workers running
length
Number of items waiting in the queue to get processed
paused
Check if queue is paused
Methods
concat(items, callbackopt, priorityopt) → {this}
concat items
onto queue - fills the queue first with items
before starting processing
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
items |
Array.<Any> | ||
callback |
function |
<optional> |
optional callback if single item was processed |
priority |
Number |
<optional> |
priority |
Returns:
for chaining
- Type
- this
drain(callbackopt) → {this}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
function |
<optional> |
optional callback called if all queue items got processed |
Returns:
for chaining
- Type
- this
pause() → {this}
Pause processing
Returns:
for chaining
- Type
- this
push(item, callbackopt, priorityopt) → {this}
push item
onto queue
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
item |
Any | ||
callback |
function |
<optional> |
optional callback if item was processed |
priority |
Number |
<optional> |
priority |
Returns:
for chaining
- Type
- this
reset() → {this}
Reset the queue by removing all pending items from the queue
Returns:
for chaining
- Type
- this
resume() → {this}
Resume processing
Returns:
for chaining
- Type
- this
running() → {Number}
Number of items being processed
Returns:
number of items processed
- Type
- Number
unshift(item, callbackopt) → {this}
put item
at the very beginnning of the queue
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
item |
Any | ||
callback |
function |
<optional> |
optional callback if item was processed |
Returns:
for chaining
- Type
- this