banner

KuchBhiLearning - A free website to learn and code

This is a good learning site. This contains details of cloud computing, AWS, AWS-CDK, AWS-SDK codes and examples including S3, Redis, lambda, api-gateway, cloudfront, cloudformation.

 Axios-Retry

If you have been coding in javascript for a while, you'd probably have known about Axios. It is a famous JS library for making HTTP request. Whether you are back-end or front-end developer, Axios is essential to access API services.

In this post, I'm going to show how to equip Axios with retry capability i.e resend HTTP request when server doesn't answer.

Axios plugin that intercepts failed requests and retries them whenever possible.

Weekly Downloads - 1,896,493

Last Published - 2 Months ago

Configurations provided by the package.

Name

Type

Default

Description

retries

Number

3

The number of times to retry before failing. 1 = One retry after first failure

retryCondition

Function

isNetworkOrIdempotentRequestError

A callback to further control if a request should be retried. By default, it retries if it is a network error or a 5xx error on an idempotent request (GET, HEAD, OPTIONS, PUT or DELETE).

shouldResetTimeout

Boolean

false

Defines if the timeout should be reset between retries

retryDelay

Function

function noDelay() { return 0; }

A callback to further control the delay in milliseconds between retried requests. By default there is no delay between retries. Another option is exponentialDelay (Exponential Backoff). The function is passed retryCount and error.

onRetry

Function

function onRetry(retryCount, error, requestConfig) { return; }

A callback to notify when a retry is about to occur. Useful for tracing. By default nothing will occur. The function is passed retryCount, error, and requestConfig.


1 import axiosRetry from 'axios-retry'; 2 3axiosRetry(axios, { 4 retries: 3, // number of retries 5 retryDelay: (retryCount) => { 6 console.log(`retry attempt: ${retryCount}`); 7 return retryCount * 2000; // time interval between retries 8 }, 9 retryCondition: (error: any) => { 10 console.log('error received at retry', error); 11 // if retry condition is not specified, by default idempotent requests are retried 12 return error.response.status === 400; 13 }, 14});

Hope this helps 👍



No comments:

Post a Comment

If you have any doubts, Please let me know

Copyright 2022, KuchBhiLearning - A free website to learn and code. All rights Reserved.
| Designed by Yaseen Shariff