It's like a group of actions that can be performed the way you want (defaults is sync and async). Just define the queue and the actions and perform it. First it'll only run in browsers.
First, add the queue script on your page
<script type="text/javascript" src="queue.js"></script>
after that, define your queue, the queue initializer accepts an object of options
var timeQueue = new Queue({ performer: 'sync' });
and then set your actions
// First action
timeQueue.add('firstAction', function(callback) {
console.log('firstAction - Started', new Date());
setTimeout((function(){
return function() {
console.log('firstAction - Ended', new Date());
callback(null, 'Hey secondAction');
}
}).call(this), 5000);
} );
all the actions have an argument called callback, you should to call it because that's how the next action will be called in sync performer. In this argument you can pass arguments for the next action or an error that'll stop the queue.
// Second action
timeQueue.add('secondAction', function(msg, callback) {
console.log('firstActions says: ' + msg);
console.log('secondAction - Started - Ended', new Date());
callback();
} );
after define the actions you can perform all of them
// Perform
timeQueue.perform();
using the sync performer you can pass a callback that'll be called when all the actions has been performed, and the last action pass some arguments too.
// Last action
timeQueue.add('lastAction', function(callback) {
console.log('lastAction - Started - Ended', new Date());
callback(null, 'That\'s all folks.');
} );
// Perform
timeQueue.perform(function(err, msg){
console.log('lastAction says: ' + msg);
console.log('All the actions has been performed.');
});
-
Basic Queue -
QueueAction -
Sync and Async performers -
Custom performer - Async perform callback
- Better perform callback results
- More Examples
- Retries
- Queue action history
Create new queue with options.
Checks if the action exists.
Add action to queue.
Remove action from queue.
Return the queue action.
Reset the action, if you reset an action you can perform it again.
Checks any action active.
Checks any action with error.
Perform the actions and call the cb function when all of them has been performed.