Skip to content

a simple promise maker which creates promises out of normal functions when you need asynchronous run of the functions

License

Notifications You must be signed in to change notification settings

pharzan/promiseRunner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Promise Runner:

A simple promisifying utility to make functions run in an asynchronous fashion.

Simply use the run() method start the promise chain and put the followed functions in order inside a .then() or add all the functions to a queue and when ever you want the queue to be executed run the .runQueue() method.

Usage:

After installing the package either globally or locally, import or require in the javascript source you need to use in and create an instence. to install locally:

npm install promiserunner # installs locally

Or to install globally make sure you have the correct privilidges,

npm install -g promiserunner # installs globally
  var PromiseRunner = require('promiserunner');
  var chain=new PromiseRunner();

There are two ways to By using the add method queue up the functions to be run asynchronously followed by the parameters the function accepts in an array in the correct order.

chain.add(f1,[1]).add(f2,[2,3]).add(f3,[5,3,5,9]).add(f3).add(f2,[100,200]).add(f5);

Keep in mind to add chain.resolve() or chain.reject() to the place you want the function to continue to the next one in line. The body of function f1 would look something like this:

var f1=function(input){
    setTimeout(function(){
     	console.log(1,input);
 	    chain.resolve(); //this is where we go to the next function
    },100);
};

when ever there is need for the queue to run use the runQueue() method to start the asynchronouse run process.

chain.runQueue();

Another method to run wrap the functions in a promise would be to use the .run() method directly:

p.run(f2)
 .then(p.run(function({
		 f1('hello');}))
 .then(p.run(f2))
 .then(p.run(f1));

Functions wrapped in a promise:

  • somewhere in the body of the function a resolve('someMessage') must be provided so that the chain doesn't break and the promise and resolves to the next function. You may also put a reject and if the function fails call .reject().
var i=1;
var f1=function one(resolve,i){ //resolve as the first parameter
	if(i==1){
		console.log('one',i);
	    chain.resolve('done'); //resolve here
    }else
    {
	    chain.reject('fail'); //rejects here
    }
};

Version

0.1.2

Todos

  • create tests
  • add stop and pause functionalities to queue runner
  • Add Code Comments

Links

About

a simple promise maker which creates promises out of normal functions when you need asynchronous run of the functions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published