Skip to content

huanguolin/repeat-runner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

repeat-runner

Build Status Coverage Status NPM Download NPM Version NPM License

A javascript tool for run repeat code.

📌 Here is 1.x version, want 0.x doc? click me.

Installing

$ npm install repeat-runner
// or
$ npm install repeat-runner --save

Example

Repeat sync code.

import RepeatRunner from 'repeat-runner';

const repeatHello = new RepeatRunner( () => console.log('hello'), 1000);

// start and stop it 3 seconds later, 
// it's equivalent to:
// repeatHello.start();
// repeatHello.stop(1000 * 3);
repeatHello.start().stop(1000 * 3);

// result: print 'hello' every second util stop 
/*
hello
hello
hello
*/

Repeat async code.
If your code contain async process, and you hope set next repeat after async process complete. Just make the function return a Promise. Of course, you can use async/await grammar.
Notice: Set third parameter to true (see API), will make runner stop when Promise#reject or any uncatched error .

// simple-1
// use Promise only
const repeatAsyncHello = new RepeatRunner( () => {
    return new Promise( (resolve, reject) => {
        setTimeout( () => {
            console.log('async hello');
            resolve();
        }, Math.random() * 5000);
    });
}, 1000);



// simple-2
// with async/await grammar
async function fetchUsers () {
    const userList = await fetch('/users');
    updateUserTable(userList);
}

const autoUpdateUsers = new RepeatRunner(fetchUsers, 20000);

function updateUserTable (userList) {
    /* ... update UI */
}



// simple-3
// stop repeat via Promise#reject
let cnt = 0;

new RepeatRunner(counting, 1000, true).start();

function counting () {
    return new Promise( (resolve, reject) => {   
        // print         
        console.log(cnt);
        // plus
        cnt++;

        if (cnt > 4) {
            reject(); // stop it 
        } else { 
            resolve(); 
        }
    });
};

// result
/*
0  // 0s
1  // 1s
2  // 2s
3  // 3s
4  // 4s, and stop now
*/

API

Create instance

Syntax

new RepeatRunner(execFunction, interval)

Parameters

execFunction: {function} this function wrap the code that need repeat
interval: {number} repeat interval (unit: ms)
stopWhenError: {boolean} configure whether to allow stop repeat when error occur(default is false)

Instance property & methods

RepeatRunner.isRunning [read-only] get current status
RepeatRunner.interval [read/write] get current interval or set new interval
RepeatRunner.execFunc [read/write] get current execFunction or set new execFunction
RepeatRunner.lastError [read-only] get last error that occur in execFunction
RepeatRunner.prototype.start(delay = -1) [return this] start runner
RepeatRunner.prototype.stop(delay = -1) [return this] stop runner

Dependencies

repeat-runner depends on a native ES6 Promise and WeakMap implementation to be supported.
If your environment doesn't support ES6 Promises, you can polyfill (Promise, Weakmap).

License

MIT