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.
npm
npm install repeated-callsyarn
yarn add repeated-callsimport 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
});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
});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 repeatedCallsAsync({ targetFunction, isComplete }).then((callCount) => {
console.log(callCount); // 3
});Both repeatedCalls and repeatedCallsAsync support stopping further attempts using stopRepeatedCalls() method:
import { repeatedCallsAsync } from 'repeated-calls';
const targetFunction = async () => {
// Simulate some async work
await new Promise((resolve) => setTimeout(resolve, 100));
return Math.random();
};
const isComplete = () => false; // Never complete
const promise = repeatedCallsAsync({ targetFunction, isComplete, callLimit: 1000 });
// Stop further calls after the current one completes
promise.stopRepeatedCalls();
promise.catch((error) => {
if (error.message === 'canceled') {
console.log('Repeated calls stopped');
}
});You can also cancel the execution immediately using cancel() method:
import repeatedCalls from 'repeated-calls';
const targetFunction = () => Math.random();
const isComplete = () => false;
const promise = repeatedCalls({ targetFunction, isComplete, callLimit: 1000 });
// Cancel immediately
promise.cancel();
promise.catch((error) => {
if (error.message === 'canceled') {
console.log('Execution canceled');
}
});Synchronous version for functions that return values directly.
Options:
targetFunction: Function to call repeatedlyisComplete: Function that determines if the result is completecallLimit(optional): Maximum number of calls (default: Infinity)delay(optional): Delay between calls in milliseconds (default: 300)isCheckBeforeCall(optional): Check completion before calling (default: true)onAfterCancel(optional): Callback after cancellation
Returns: Promise with methods:
cancel(): Cancel execution immediatelystopRepeatedCalls(): Stop further attempts after current call completes
Asynchronous version for functions that return Promises.
Options:
targetFunction: Async function to call repeatedlyisComplete: Function that determines if the result is completecallLimit(optional): Maximum number of calls (default: Infinity)delay(optional): Delay between calls in milliseconds (default: 300)isCheckBeforeCall(optional): Check completion before calling (default: true)isRejectAsValid(optional): Treat rejections as valid results (default: false)onAfterCancel(optional): Callback after cancellation
Returns: Promise with methods:
cancel(): Cancel execution immediatelystopRepeatedCalls(): Stop further attempts after current call completes
npm testKrivega Dmitriy
- Website: https://krivega.com
- Github: @Krivega
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Copyright © 2020 - 2025 Krivega Dmitriy.
This project is MIT licensed.