npm install queued-up
npm install queued-up
.queue() - Returns the queue array
.queue(\[,...\]) - Sets queue array to the input replacing existing items
.add(input) - Adds the input to the end of the queue array
.remove(number)- removes the queue item at the given index
.index - returns the current iteration point of the queue
.index(number) - sets the index manually
.next() - processes the next item in the queue, and returns result.
.next(n) - Processes the next n items in the queue. Get results from .results()
.run() - begins processing queue while maintaining the queue
.run(index) - begins processing queue at given index
.shift() - Processes the first item in the queue, removes it, and returns the results
.shift(n) - Performs .shift() for the next n items. Get results from .results()
.shiftRun() - begins processing all items in the queue, removing each, and appending .results();
.pause() - pauses the run() at current index or shiftRun().
.resume() - resumes the queue run. Do not call this within the 'paused' eventhandler!
.reset() - sets index(), queue(), and results() to zero and empty
.results() - Returns the results array. Index matching run/next, or order of shift/shiftRun
.shiftResults()- Returns the first item in the results array and removes it.
javascript
var qup = require('queued-up');
/* Define the function called for each iteration.
* End all action functions with this.done() to notify completion.
* Passing this.done() with an argument will add the data to the results array queue.results().
* Running this.done() as the return function will also return the data in .next() and .shift().
*/
function square(item){
return this.done(item*item);
}
//Pass the action into the module, and a new queue object will be created.
var queue = new qup(square);
//add the items to the queue
queue.add([1,2,3,4,5,6]);
console.log(queue.next()); //returns 1
console.log(queue.next()); //returns 6
console.log(queue.next()); //returns 9
//.results() returns the array of results where the index
//matches the queued item.
console.log(queue.results());//returns [ 1, 4, 9 ]
//.queue returns the contents of the queued items.
console.log(queue.queue());//returns [ 1, 2, 3, 4, 5, 6 ]
`
$3
`javascript
var qup = require('queued-up');
/* Define the function called for each iteration.
* End all action functions with this.done() to notify completion.
* Passing this.done() with an argument will add the data to the results array queue.results().
* Running this.done() as the return function will also return the data in .next() and .shift().
*/
function square(item){
return this.done(item*item);
}
//Pass the action into the module, and a new queue object will be created.
var queue = new qup(square);
//add the items to the queue
queue.add([1,2,3,4,5,6]);
console.log(queue.shift()); //returns 1
console.log(queue.shift()); //returns 6
console.log(queue.shift()); //returns 9
//.results() after a shift() returns the items in the order they were operated on.
console.log(queue.results());//returns [ 1, 4, 9 ]
//.queue after shift shows the remaining items.
console.log(queue.queue());//returns [ 4, 5, 6 ]
`
$3
`javascript
var qup = require('queued-up');
function square(item){
return this.done(item*item);
}
var queue = new qup(square);
//add the items to the queue
queue.add([1,2,3,4,5,6]);
//Run will run the action function for all items.
queue.run();
console.log(queue.results());//returns [ 1, 4, 9, 16, 25, 36 ]
console.log(queue.queue());//returns [ 1, 2, 3, 4, 5, 6 ]
`
$3
`javascript
var qup = require('queued-up');
function square(item){
return this.done(item*item);
}
var queue = new qup(square);
//add the items to the queue
queue.add([1,2,3,4,5,6]);
//Run will run the action function for all items.
queue.shiftRun();
console.log(queue.results()); //returns [ 1, 4, 9, 16, 25, 36 ]
console.log(queue.queue()); //returns []
`
$3
Note that this would only return the last item. See .results() for the results of the processing. This can be useful if you wish to process items in chunks.
`javascript
//will process the next 3 items in the queue.
queue.next(3);
//will process and remove the next 3 items.
queue.shift(3);
``