-
Notifications
You must be signed in to change notification settings - Fork 2
Retries
DigitalBrainJS edited this page Oct 17, 2021
·
1 revision
Use CPromise.retry
to retry async operations (3 attempts by default) with a delay(by default delay = attempt * 1000ms)
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
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.