repeated-calls
v2.3.0
Published
Repeated calls to the function
Downloads
1,365
Readme
repeated-calls
Repeated calls to the function
A stack of tasks that are executed one by one, but the result is taken from the last. Identical functions on the stack (check by reference) are executed only once.
Install
npm
npm install repeated-calls
yarn
yarn add repeated-calls
Usage
import repeatedCalls from 'repeated-calls';
const targetFunction = function innerTargetFunction() {
innerTargetFunction.count = innerTargetFunction.count || 0;
innerTargetFunction.count += 1;
return innerTargetFunction.count;
};
const isComplete = (callCount) => callCount === 3;
return repeatedCalls({ targetFunction, isComplete }).then((callCount) => {
console.log(callCount); // 3
});
Complete if the limit is reached
import repeatedCalls from 'repeated-calls';
const targetFunction = function innerTargetFunction() {
innerTargetFunction.count = innerTargetFunction.count || 0;
innerTargetFunction.count += 1;
return innerTargetFunction.count;
};
const isComplete = (callCount) => callCount === 3;
const callLimit = 3;
return repeatedCalls({ targetFunction, isComplete, callLimit }).catch((error) => {
console.log(error); // call limit (3) is reached
});
Use async targetFunction
import { repeatedCallsAsync } from 'repeated-calls';
const targetFunction = function innerTargetFunction() {
innerTargetFunction.count = innerTargetFunction.count || 0;
innerTargetFunction.count += 1;
return Promise.resolve(innerTargetFunction.count);
};
const isComplete = (callCount) => callCount === 3;
return repeatedCalls({ targetFunction, isComplete }).then((callCount) => {
console.log(callCount); // 3
});
Run tests
npm test
Maintainer
Krivega Dmitriy
- Website: https://krivega.com
- Github: @Krivega
Contributing
Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.
📝 License
Copyright © 2020 Krivega Dmitriy. This project is MIT licensed.