Skip to content

Instantly share code, notes, and snippets.

@szanata
Last active August 29, 2018 10:56
Show Gist options
  • Save szanata/7a69731a44d42bc2a8dd1201fef82356 to your computer and use it in GitHub Desktop.
Save szanata/7a69731a44d42bc2a8dd1201fef82356 to your computer and use it in GitHub Desktop.
Every (loop tool for node js)
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 );
}
};
}
};
};
{
"name": "every",
"version": "0.1.0",
"main": "every.js"
}
@szanata
Copy link
Author

szanata commented Aug 29, 2018

How to use

const every = require('every')

const result = await every( 250 ).do( async () => asyncStuff() ).times( 200 );

So every X miliseconds (250 in the example), it will run the do fn until X times (200 in the example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment