Last active
August 29, 2018 10:56
-
-
Save szanata/7a69731a44d42bc2a8dd1201fef82356 to your computer and use it in GitHub Desktop.
Every (loop tool for node js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = interval => { | |
let count = 0; | |
return { | |
do( fn ) { | |
return { | |
async times( amount ) { | |
const jobs = []; | |
await new Promise( ( resolve, reject ) => { | |
const loop = setInterval( async () => { | |
count++; | |
if ( count === amount ) { | |
clearInterval( loop ); | |
resolve(); | |
} else { | |
jobs.push( fn() ); | |
} | |
}, interval ); | |
} ); | |
return Promise.all( jobs ); | |
} | |
}; | |
} | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "every", | |
"version": "0.1.0", | |
"main": "every.js" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use
So every X miliseconds (250 in the example), it will run the do fn until X times (200 in the example).