1.1.1 • Published 9 years ago

whiletrue v1.1.1

Weekly downloads
1
License
MIT
Repository
-
Last release
9 years ago

whiletrue

Start/stop/pause/repeat: task runner in Javascript

Usage

Given the following iteration condition...

//Condition: iterate 10 times
function condition(state){

    //In 'state.index' is contained iteration 
    //number '0-based'
    return state.index < 10;
}

and the following task to run, over and over...

//Task: do some work, then invoke "done"
function longTask(done, state){

    //Do some stuff here...
    console.log("Doing some stuff...");
    
    //Argument "state" => ex: { 
    //  iterations: 5,  //Current iteration number 
    //  delay: 10       //Delay (in ms) between iterations
    //}

    setTimeout(function(){

        //Task completed: remember to invoke ALWAYS "done"
        console.log("Task completed!");
        done();

    }, 1000);
}

just setup the "whiletrue" runner (with 10 millisecond of delay between each iteration)...

//Setup: Create a "whiletrue" runner with 10 ms delay
var runner = whiletrue(condition, longTask, 10);

start the execution and handle the promise of "job completed"...

//Start execution
runner.run()
    .then(function(){
        console.log("Job completed");
    });

...or start the execution in parallel mode...

//Start execution
runner.runParallel()
    .then(function(){
        console.log("Job completed");
    });
1.1.1

9 years ago

1.1.0

9 years ago

1.0.2

9 years ago