Skip to content

Axios retry example

Dmitriy Mozgovoy edited this page May 24, 2021 · 2 revisions

CodeSandBox Demo

Promise.retry(() => cpAxios(url)).then(console.log)
// or in the context of CPromise async function
yield Promise.retry(() => cpAxios(url));
// or in the context of ECMA async function
await Promise.retry(() => cpAxios(url));

A more complex example:

import { CPromise } from "c-promise2";
import cpAxios from "cp-axios";

const url =
  "https://run.mocky.io/v3/7b038025-fc5f-4564-90eb-4373f0721822?mocky-delay=2s";

const promise = CPromise.retry((attempt) => {
  console.log(`Attempt [${attempt}]`);
  return cpAxios(url).timeout(attempt * 1000 + 500);
}).then(
  (response) => console.log(`Response: ${JSON.stringify(response.data)}`),
  (err) => console.warn(`Fail: ${err}`)
);

// promise.pause()
// promise.resume()
// promise.cancel()
Clone this wiki locally