Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

f-queue

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

f-queue

fqueue is a micro-plugin to queue function execution to handle asyncronous flow and stepping though functions. So you save yourself with ugly callbacks and can make your code more preety.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

fqueue

fqueue is a micro-plugin to queue function execution to handle asyncronous flow and stepping though functions.

  • It support adding or removing of function after initialization of queue.
  • Support storing of data which can be used to all function whithin that queue
  • Support parrallel and series async call.
  • Give full control over queue like, stop , start (from any index), hold, continue, call next function.
  • Can define ignore indexes if you are running queue again and don't want to run ignore some functions.
  • Make your code more clean (especially if you are node - mongo developer it can save you from dirty callbacks)

** fqueue passes queue object as this in all function but be sure to store this on some variable and use that inside any callback function, because there value of this will be different.

Installation

For node

npm install f-queue
	

For browser just iclude fqueue.min.js file or fqueue.js in your directory.

Methods

MethodsParametersDescription
nextAny number of arguments which you want to pass on next function of queue.Execute the next method in queue.
add1. function * : function you want to add
2. pos (optional) : Index at which function will be add. If not defined, add function on last.
To add function on the queue after initialization.
remove1. index * : Index of function which you want to remove.Remove a function at particular index from a queue.
holdno parameterTo hold the queue to go next item if autoStep option is enabled.
continueno parameterContinue the queue to go to next item.
completeAny number of arguments which you want to pass on next function of queue.To tell an async call have been finished
start

index * : Index at which you want to start the queue.

Any arguments starting followed by first argument will be passed to to function through which queue is starting.

To start the queue.
stopno parameterTo stop the queue.
ignoreidxAry :  A array of indexes for which you don't want to execute your function.To define array of indexes to ignore.


Options

OptionsDefaultParameters
autoStarttrueAuto start a queue.
autoSteptrueAuto step to next function in a queue on finish of a function execution.
startParams[] - empty arrayAn array of parameters which you want to pass on first function.

You can change the defauls globally like


    fqueue.defaults.autoStart=false;  
   

Examples

Simple Example

var queue=fqueue(
function(){
    //do somthing synchronous
    },
function(){
    //do somthing synchronous
    },
function(){
    //do somthing synchronous
    }		
);
  
In this example queue will start during initialization and auto step through each function. If you don't want so you can disable those options like.
var queue=fqueue({autoStart:false,autoStep:false}
function(){
    //do somthing synchronous
    },
function(){
    //do somthing synchronous
    },
function(){
    //do somthing synchronous
    }		
);
  

And then when you want you can start it using queue.start(index) and step through functions using queue.next();

You can use .add() method to add function in queue after initializtion of queue or inside a function of queue.

var queue=fqueue({autoStart:false}
function(){
    //do somthing synchronous
    },
function(){
    //do somthing synchronous
    //to get current index
    var index=this.current;
    //to add function next to current function
    this.add(function(){
            //do something else 
        },++index);
    },
function(){
    //do somthing synchronous
    }		
);

or queue.add(function(){ //do something else });

Same you can use .remove() method.

You can step to next method in middle of any function too.

var queue=fqueue(
function(){
    //do somthing synchronous
    this.next();
    //do something else
    },
function(){
    //do somthing synchronous
    },
function(){
    //do somthing synchronous
    }		
);
  

** However if you are using .next() method when execution of the function completes it bypass all those function which are already been called by .next() method.

To run asynchronous function you need two methods .hold() and .complete() if autoStep is enabled or you want to run parrallel async functions, or you can use simply use next method to go next function in the queue after the complete of sync function.

var queue=fqueue(
function(){
    queue.hold();
    $.get('get.php',function(){
            //do something
            //you can pass parameter to next function 
            queue.complete('Sudhanshu','23');
        });
    },
function(name,age){
    //do somthing synchronous
    },
function(){
    queue.hold();
    $.get('get.php',function(){
            //do something
            queue.complete();
        });
    }		
);

or

var queue=fqueue({autoStep:false}
function(){
    $.get('get.php',function(){
            //do something
            //you can pass parameter to next function 
            queue.next('Sudhanshu','23');
        });
    },
function(name,age){
    //do somthing synchronous
     queue.next();
    },
function(){
    //you can also use this instead of queue name, but be sure to save it on some variable
    // because "this" will change inside callback function.
    self=this;
    $.get('get.php',function(){
            //do something
            self.next();
        });
    }		
);
  

You can also run async functions in parrallel by sending those functions in a array.

var queue=fqueue(
//send function in array to run them in parrallel
[function(){
    queue.hold();
    $.get('get.php',function(){
            //do something
            queue.complete();
        });
    },
function(){
    queue.hold();
    $.get('get.php',function(){
            //do something
            queue.complete();
        });
    }],
function(){
    //this will be executed after first two parrallel function's execution completes.
    //do somthing synchronous
    }		
);
  

You can use .data,.previousResult,.results to store data, get return value of previous function and to get result of all functions.

var queue=fqueue(
function(){
    //do somthing synchronous
    var name="Sudhanshu";
    this.data.name=name;
    return 1;
    },
function(){
    //do somthing synchronous
    console.log(this.data.name);
    //will log Sudhanshu
    return 2;
    },
function(){
    //do somthing synchronous
    console.log(this.previousResult);
    //will log 2;
    
    return 3;
    },
function(){
    //do something synchronous
    return 4;
}    
            
);

console.log(queue.results);
//will log [1,2,3,4];

  

In some case you want to call the same queue but you want to ignore some functions in the queue. Suppose the first function was used only to get name and third function only log was done and while calling again the queue you want to ignore first and third function.

queue.ignore([0,2]) //you have to give the index of function in a array which you want to ignore.
queue.start(0); //this will ignore the first and third function.

Keywords

FAQs

Package last updated on 11 Nov 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc