@prakshark/async-retry-wrapper
v2.0.1
Published
An async retry utility for JavaScript/Node.js. Retries a given async function multiple times on failure.
Maintainers
Readme
@prakshark/async-retry-wrapper
An async retry utility for JavaScript/Node.js. Retries a given async function multiple times on failure.
Installation
npm install @prakshark/async-retry-wrapperUsage
import asyncRetry from "@prakshark/async-retry-wrapper";
async function someFunction(someParameter) {
// This user defined function will return a promise.
}
asyncRetry(() => someFunction("someArgument"), { retries: 5 })
.then(data => console.log("Fetched data:", data))
.catch(err => console.error("All retries failed:", err));Usage Example Snippet
import asyncRetry from "@prakshark/async-retry-wrapper";
async function fetchGithubData(username) {
let response = await fetch(`https://api.github.com/users/${username}`);
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status}`);
}
let data = await response.json();
return data;
}
asyncRetry(() => fetchGithubData("prakshark"), { retries: 5 })
.then(data => console.log("Fetched data:", data))
.catch(err => console.error("All retries failed:", err));API
asyncRetry(fn, options)
fn: An async function to retry.options.retries: Number of retry attempts (default: 3).
Returns a Promise that resolves with the result of the function, or rejects after all retries fail.
