Retry Logic Wrapper

JS
S
JavaScript

Reusable code pattern for implementing automatic retry mechanisms in error-prone operations, enhancing application resilience and fault tolerance.

1const retry = require('retry');
2
3const operation = retry.operation({
4  retries: 5,
5  factor: 3,
6  minTimeout: 1 * 1000,
7  maxTimeout: 60 * 1000,
8  randomize: true,
9});
10
11operation.attempt(async (currentAttempt) => {
12  console.log('sending request: ', currentAttempt, ' attempt');
13  try {
14
15    await axios.put(...);
16
17  } catch (e) {
18    if (operation.retry(e)) { return; }
19  }
20});

Created on 11/10/2021