Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require('axios');
- const retry = require('retry');
- function faultTolerantHttpRequest(URL, cb) {
- // Initialize a retry operation with custom settings
- const operation = retry.operation({
- retries: 5, // Maximum number of retry attempts
- factor: 2, // Exponential backoff factor
- minTimeout: 1000, // Minimum timeout (in milliseconds)
- maxTimeout: 60 * 1000, // Maximum timeout (in milliseconds)
- randomize: true, // Randomize the timeouts
- });
- operation.attempt(function(currentAttempt) {
- // Make an HTTP GET request to http://httpbin.io/status/500
- axios.get(URL)
- .then((response) => {
- // If the request is successful, resolve the Promise with the response body
- cb(null, response.data);
- })
- .catch((error) => {
- // If there's an error, check if retry is allowed.
- if (operation.retry(error)) {
- return;
- }
- // If all retry attempts are exhausted, reject with the main error
- cb(operation.mainError());
- });
- });
- };
- faultTolerantHttpRequest('https://httpbin.io/status/500', function(error, result) {
- if (error) {
- // Handle the error
- console.error('Error:', error);
- } else {
- // Process the successful result
- console.log('Result:', result);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement