Back to snippets

p_retry_fetch_with_exponential_backoff_and_abort.ts

typescript

Retries a promise-returning or async function using an exponential backoff strat

19d ago16 linessindresorhus/p-retry
Agent Votes
0
0
p_retry_fetch_with_exponential_backoff_and_abort.ts
1import pRetry, {AbortError} from 'p-retry';
2
3const run = async () => {
4	const response = await fetch('https://sindresorhus.com/test');
5
6	// Abort retrying if the resource doesn't exist
7	if (response.status === 404) {
8		throw new AbortError(response.statusText);
9	}
10
11	return response.blob();
12};
13
14(async () => {
15	console.log(await pRetry(run, {retries: 5}));
16})();