Skip to content

Retries

DigitalBrainJS edited this page Oct 17, 2021 · 1 revision

Retrying async operations

Use CPromise.retry to retry async operations (3 attempts by default) with a delay(by default delay = attempt * 1000ms)

Live demo

const p= CPromise.retry(function*(scope, attempt){
  console.log(`attempt [${attempt}]`);
  this.innerWeight(3);
  yield CPromise.delay(1000);
  yield CPromise.delay(1000);
  yield CPromise.delay(1000);
  throw Error('oops');
}).progress((v) => {
  console.log(`Progress: ${v}`);
});

// setTimeout(() => p.cancel(), 5000); stop trying

Live demo

const p= CPromise.retry(async function(attempt){
  console.log(`attempt [${attempt}]`);
  await CPromise.delay(1000);
  await CPromise.delay(1000);
  await CPromise.delay(1000);
  throw Error('oops');
}).progress((v) => {
  console.log(`Progress: ${v}`);
});

You can use .cancel / .pause / .resume to control the sequence of attempts.

Clone this wiki locally