@coxy/reply-count-loop

Repeats function calls until there is a result

Usage no npm install needed!

<script type="module">
  import coxyReplyCountLoop from 'https://cdn.skypack.dev/@coxy/reply-count-loop';
</script>

README

Register function

const random = () => {
  if (Math.random() > 0.01) {
    throw new Error(':(');
  }
  return 'Success!';
}

Add to loop

import replayCountLoop from '@coxy/reply-count-loop';

const loop = replayCountLoop(random, {
  onError: e => console.log('error', e.message),
  attempts: 20,
  delay: 100,
})

loop.then(console.log);
loop.catch(console.error);