A very simple and small services to guarantee a Unique Execution
of some piece of code (async) in
a javascript / typescript applications.
This services use some environment variables to pre-adjust some things, like:
UNIQUE_START_DELAY
: define a minimum delay to start func
execution;UNIQUE_POOLING
: pooling time to check delayed func
to execute;The delay time is a mininum
delay time, checked at UNIQUE_POOLING
interval.
import { uniqueExecution } from '@nsfilho/unique';
/**
* Simple async function for example.
* Show a message after certain amount of time.
*/
const delayMessage = async (message: string, delay: number): Promise<void> =>
new Promise((resolve) => {
setTimeout(() => {
console.log(message);
resolve();
}, delay);
});
uniqueExecution({
name: __filename,
callback: async () => {
// execute a async func
await delayMessage('Hello World!!', 2000);
},
});
Generated using TypeDoc