feat: add 'exec and check' helper
useful for re-executing some function if the expected result isn't found
This commit is contained in:
21
src/lib/exec_and_check.mjs
Normal file
21
src/lib/exec_and_check.mjs
Normal file
@@ -0,0 +1,21 @@
|
||||
export default async function execAndCheck(fn, test, onFailure = () => {}, opts = { tries: 3 }) {
|
||||
let count = 0;
|
||||
|
||||
const tryExec = async () => {
|
||||
// exec function
|
||||
await fn();
|
||||
|
||||
// check test function result (pass/fail)
|
||||
const res = await test();
|
||||
if (res) return;
|
||||
|
||||
// if retry limit is hit, call failure function
|
||||
count += 1;
|
||||
if (count >= opts.tries) return onFailure(); // eslint-disable-line consistent-return
|
||||
|
||||
// retry the function and retest the results
|
||||
return tryExec(); // eslint-disable-line consistent-return
|
||||
};
|
||||
|
||||
return tryExec();
|
||||
}
|
||||
Reference in New Issue
Block a user