JSFiddle - React, Tailwind, and code Playground
by joplomacedo
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
JavaScript
function Processor ( f, args_def ) {
const in_process = {};
return function () {
let process_id;
if ( !Array.isArray(args_def) || args_def.length ) {
process_id = _.reduce(arguments, (acc, value) => {
return acc + value;
}, '');
} else {
process_id = _.reduce(args_def, (acc, value, i) => {
return acc + (value ? arguments[i] : '');
}, '');
}
console.log('process_id', process_id);
if ( in_process[process_id] ) {
return Promise.reject('already_in_process');
}
in_process[process_id] = 1;
return f(...arguments).finally( () => { delete in_process[process_id] });
};
}
const y = Processor(function ( price, quantity) {
return new Promise(( resolve, reject ) => {
setTimeout( () => {
resolve(price*quantity);
}, 4000);
});
}, [false, false]);
y(2,3).then( res => {
console.log(res);
});
y(2,3).then( res => {
console.log(res);
});
setTimeout( () => {
y(2,3).then( res => {
console.log(res);
});
}, 3000)