Code Monkey home page Code Monkey logo

queue.js's Introduction

Queue.js

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.

Table of contents

How to use

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.');
});

Checklist

  • Basic Queue
  • QueueAction
  • Sync and Async performers
  • Custom performer
  • Async perform callback
  • Better perform callback results
  • More Examples
  • Retries
  • Queue action history

API

new Queue(options)

Create new queue with options.

Queue.hasAction(name)

Checks if the action exists.

Queue.add(name, fn, options)

Add action to queue.

Queue.remove(name)

Remove action from queue.

Queue.action(name)

Return the queue action.

Queue.resetAction(name)

Reset the action, if you reset an action you can perform it again.

Queue.anyActive()

Checks any action active.

Queue.anyError()

Checks any action with error.

Queue.perform(cb)

Perform the actions and call the cb function when all of them has been performed.

queue.js's People

Contributors

gabrielcorado avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.