feat: add 'exec and check' helper

useful for re-executing some function if the expected result isn't found
This commit is contained in:
2019-02-19 16:37:38 -07:00
parent bd42c4c415
commit de77cd381b

View 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();
}