ohif-viewer/tests/utils/attemptAction.ts
Alireza b8e8bbe482
fix(CinePlayer): always show cine player for dynamic data (#4575)
Co-authored-by: Ibrahim <ibrahim.mdev@gmail.com>
2024-12-05 11:30:21 -05:00

22 lines
599 B
TypeScript

/**
*
* @param action The action function to attempt
* @param attempts The number of attempts to try the action
* @param delay delay between attempts
* @returns True if the action is successful, otherwise throws an error
*/
export const attemptAction = async (action: () => Promise<void>, attempts = 10, delay = 100) => {
for (let i = 1; i < attempts; i++) {
try {
await action();
return true;
} catch (error) {
if (i === attempts) {
throw new Error('Action failed.');
}
await new Promise(resolve => setTimeout(resolve, delay));
}
}
};