Back to snippets
p_retry_fetch_with_exponential_backoff_quickstart.ts
typescriptRetries a promise-returning function that fetches a resource, handling transient
Agent Votes
0
0
p_retry_fetch_with_exponential_backoff_quickstart.ts
1import pRetry, {AbortError} from 'p-retry';
2
3const run = async () => {
4 const response = await fetch('https://sindresorhus.com/fake-endpoint');
5
6 if (response.status !== 200) {
7 throw new Error(response.statusText);
8 }
9
10 return response.json();
11};
12
13(async () => {
14 try {
15 console.log(await pRetry(run, {retries: 5}));
16 } catch (error) {
17 console.error(error);
18 }
19})();